- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于下面的代码片段,我想了解 NodeJS 运行时如何处理事情:
const billion = 1000000000;
function longRunningTask(){
let i = 0;
while (i <= billion) i++;
console.log(`Billion loops done.`);
}
function longRunningTaskProm(){
return new Promise((resolve, reject) => {
let i = 0;
while (i <= billion) i++;
resolve(`Billion loops done : with promise.`);
});
}
function longRunningTaskPromResolve(){
return Promise.resolve().then(v => {
let i = 0;
while (i <= billion) i++;
return `Billion loops done : with promise.resolve`;
})
}
console.log(`*** STARTING ***`);
console.log(`1> Long Running Task`);
longRunningTask();
console.log(`2> Long Running Task that returns promise`);
longRunningTaskProm().then(console.log);
console.log(`3> Long Running Task that returns promise.resolve`);
longRunningTaskPromResolve().then(console.log);
console.log(`*** COMPLETED ***`);
longRunningTask() function will block the main thread, as expected.
In longRunningTaskProm() wrapping the same code in a Promise, was expecting execution will move away from main thread and run as a micro-task. Doesn't seem so, would like to understand what's happening behind the scenes.
Third approach longRunningTaskPromResolve() works.
Creation and execution of a Promise is still hooked to the main thread. Only Promise resolved execution is moved as a micro-task.
最佳答案
所有这三个选项都在主线程中运行代码并阻止事件循环。当他们开始运行 while
时,时间略有不同。循环代码以及它们何时阻塞事件循环,这将导致它们与某些控制台消息的运行时间不同。
第一个和第二个选项立即阻止事件循环。
第三个选项阻止事件循环从下一个滴答开始 - 那是 Promise.resolve().then()
调用您传递给 .then()
的回调(在下一个刻度)。
第一个选项只是纯同步代码。毫不奇怪,它会立即阻塞事件循环,直到 while
循环完成。
在第二个选项中,新的 Promise 执行器回调函数也被同步调用,因此它再次立即阻塞事件循环,直到 while
循环完成。
在第三个选项中,它调用:
Promise.resolve().then(yourCallback);
Promise.resolve()
创建一个已经解决的 promise ,然后调用
.then(yourCallback)
在那个新的 promise 上。此日程安排
yourCallback
在事件循环的下一个滴答上运行。根据 promise 规范,
.then()
处理程序总是在事件循环的 future 滴答上运行,即使 promise 已经解决。
yourCallback
.但是,当它确实运行该回调时,它会在主线程中运行,因此会阻塞直到它完成。
Creation and execution of a Promise is still hooked to the main thread. Only Promise resolved execution is moved as a micro-task.
.then()
处理程序计划在事件循环的 future 滴答中运行(仍在主线程中)。这种调度使用了一个微任务队列,它允许它在事件队列中的其他一些东西之前得到它,但它仍然在主线程中运行并且它仍然在事件循环的 future 滴答上运行。
.then()
计划在将来的某个时间使用它们运行回调。或
.catch()
或
.finally()
在一个 promise 上。因此,一般而言,您不想考虑“执行 promise ”。您的代码执行导致创建一个 promise ,然后您注册该 promise 上的回调以根据该 promise 发生的情况在 future 运行。 Promises 是一个专门的事件通知系统。
setTimeout(fn, 1)
在第三个选项之后,看到超时被阻止运行,直到第三个选项完成。这是一个例子。而且,我已将阻塞循环全部设为 1000 毫秒长,以便您可以更轻松地看到。在这里在浏览器中运行它或复制到 node.js 文件并在那里运行它以查看
setTimeout()
被
longRunningTaskPromResolve()
的执行时间阻止按时执行.所以,
longRunningTaskPromResolve()
还在堵。将其放入
.then()
处理程序在运行时会发生变化,但它仍然处于阻塞状态。
const loopTime = 1000;
let startTime;
function log(...args) {
if (!startTime) {
startTime = Date.now();
}
let delta = (Date.now() - startTime) / 1000;
args.unshift(delta.toFixed(3) + ":");
console.log(...args);
}
function longRunningTask(){
log('longRunningTask() starting');
let start = Date.now();
while (Date.now() - start < loopTime) {}
log('** longRunningTask() done **');
}
function longRunningTaskProm(){
log('longRunningTaskProm() starting');
return new Promise((resolve, reject) => {
let start = Date.now();
while (Date.now() - start < loopTime) {}
log('About to call resolve() in longRunningTaskProm()');
resolve('** longRunningTaskProm().then(handler) called **');
});
}
function longRunningTaskPromResolve(){
log('longRunningTaskPromResolve() starting');
return Promise.resolve().then(v => {
log('Start running .then() handler in longRunningTaskPromResolve()');
let start = Date.now();
while (Date.now() - start < loopTime) {}
log('About to return from .then() in longRunningTaskPromResolve()');
return '** longRunningTaskPromResolve().then(handler) called **';
})
}
log('*** STARTING ***');
longRunningTask();
longRunningTaskProm().then(log);
longRunningTaskPromResolve().then(log);
log('Scheduling 1ms setTimeout')
setTimeout(() => {
log('1ms setTimeout Got to Run');
}, 1);
log('*** First sequence of code completed, returning to event loop ***');
1 0.000: *** STARTING ***
2 0.005: longRunningTask() starting
3 1.006: ** longRunningTask() done **
4 1.006: longRunningTaskProm() starting
5 2.007: About to call resolve() in longRunningTaskProm()
6 2.007: longRunningTaskPromResolve() starting
7 2.008: Scheduling 1ms setTimeout
8 2.009: *** First sequence of code completed, returning to event loop ***
9 2.010: ** longRunningTaskProm().then(handler) called **
10 2.010: Start running .then() handler in longRunningTaskPromResolve()
11 3.010: About to return from .then() in longRunningTaskPromResolve()
12 3.010: ** longRunningTaskPromResolve().then(handler) called **
13 3.012: 1ms setTimeout Got to Run
longRunningTask()
发起。 longRunningTask()
完成。它是完全同步的。 longRunningTaskProm()
发起。 longRunningTaskProm()
电话resolve()
.从中可以看出promise executor函数(传递给new Promise(fn)`的回调也是完全同步的。longRunningTaskPromResolve()
发起。您可以看到来自 longRunningTaskProm().then(handler)
的处理程序还没有被调用。它已被安排在事件循环的下一个滴答上运行,但由于我们还没有回到事件循环,因此还没有被调用。 longRunningTaskPromResolve()
.那是因为 longRunningTaskPromResolve()
还没有做太多。它跑了 Promise.resolve().then(handler)
,但所做的只是安排了 handler
在事件循环的 future 滴答上运行。所以,只用了 1 毫秒来安排它。该函数的长时间运行部分尚未开始运行。 longRunningTaskProm().then(handler)
的处理程序。这样就被调用了。您可以看到它已经在等待运行,因为它在我们返回到事件循环后仅运行了 1 毫秒。该处理程序运行,我们返回到事件循环。 Promise.resolve().then(handler)
的处理程序。所以我们现在看到它开始运行,因为它已经排队,它在前一个事件完成后立即运行。 longRunningTaskPromResolve()
中的循环正好需要 1000 毫秒运行然后它从它的 .then()
返回然后调度下一个的处理程序 .then()
该 promise 链中的处理程序在 eventl 循环的下一个滴答上运行。 .then()
得到运行。 .then()
计划运行的处理程序,setTimeout()
回调开始运行。它被设置为在 1 毫秒内运行,但它被所有以更高优先级运行的 promise 操作延迟了,所以它没有运行 1 毫秒,而是在 1004 毫秒内运行。 关于javascript - 返回新的 Promise 和 Promise.resolve 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61637464/
我遇到了一个奇怪的问题。我的应用程序的 Release 版本似乎运行良好,但最近当我切换到 Debug 版本时,我在启动时立即遇到访问冲突。当释放分配的内存块时,就会发生访问冲突。所有这些都发生在静态
我在 C# 中偶然发现了这种奇怪的语法形式,并试图弄清楚它的含义以及如何使用它。网络上似乎没有关于此的任何文档。 object data = new { var1 = someValue, var2
我正在尝试使用浏览器的内置类型 CSSStyleDeclaration 以编程方式传递和修改样式(由于 .cssText 属性,这很方便)。 但是,new CSSStyleDeclaration()
我有现成的代码: internal bool firstAsSymbol(out Symbol s) { return (s = first as Symbol) !=
在新的 Eclipse 版本 2022-03 中,一些(但不是全部)java 项目在 Project Explorer View 中的外观发生了变化。尽管 Package Presentation 设
我正在尝试使用 FormData 通过获取 API 在 POST 请求中发送用户输入的数据。问题是,当我用我创建的表单创建一个新的 FormData 对象时,它一直在创建一个空对象——没有条目/键/值
我有一个用一些 intel-intrinsincs 编写的 C 代码。在我先用 avx 然后用 ssse3 标志编译后,我得到了两个完全不同的汇编代码。例如: AVX: vpunpckhbw %xm
最近,discord 为您自己的应用程序添加了对斜杠命令的支持。我通读了它的文档,并尝试搜索一些视频(但是该功能刚刚出现),但我不明白我实际上需要做什么才能使其正常工作。我正在使用 WebStorm(
我想使用 JRI 从 Java 调用 R。 我在 eclipse 下在主类中运行它: Rengine c = new Rengine(new String[] { "--vanilla" },
我正在使用新的 Place Autocomplete那是来自新的静态Google Places SDK 客户端库 (here)。所以它真的很容易使用,我刚得到this tutorial它按预期工作。
我刚刚更新到 flutter 版本 1.25.0-5.0.pre.92,我的代码中出现了很多与空安全相关的错误,这些错误以前运行良好。我没有以任何方式选择空安全,我所做的只是运行 flutter 升级
我已经使用 React Native 有一段时间了,但我想我会在网络上试用 React。所以我遵循了这个指南:https://reactjs.org/docs/create-a-new-react-a
周六早上在这里。尝试学习新的 Scala 编译器 dotty。 安装在我的 Mac 上使用 brew install lampepfl/brew/dotty 安装成功。我有版本 dotr -versi
我使用了谷歌地方的新依赖。单击自动完成 View 时应用程序崩溃。错误如下。, java.lang.NullPointerException: Place Fields must be set.
我关注了这个博客-> https://medium.com/@teyou21/training-your-object-detection-model-on-tensorflow-part-2-e9e
在哪里可以找到用于在此架构上进行组装的新寄存器的名称? 我指的是 X86 中的寄存器,如 EAX、ESP、EBX 等。但我希望它们是 64 位的。 我认为它们与我反汇编 C 代码时不同,我得到的是 r
新的服务总线库 Azure.Messaging.ServiceBus 使用 ServiceBusReceivedMessage 来接收消息 https://learn.microsoft.com/en
需要使用实时流媒体 channel 的实时编码类型在新的 Azure 门户中配置广告插入和石板图像。请帮忙解决这个问题,因为我找不到该功能。 最佳答案 此处描述了 Azure 媒体服务的广告插入选项
我正在使用新的 GitHub 操作,下面的工作流程的想法是在打开或同步 pr 时运行,它应该首先检查并安装依赖项,然后运行一些 yarn 脚本 name: PR to Master on: pul
我听说 DMD 2.058 中将有一个用于匿名函数的新语法,但我找不到任何相关信息。新语法是什么?旧语法是否会被弃用? 最佳答案 我相信它就像 C#'s . 以下内容是等效的: delegate(i,
我是一名优秀的程序员,十分优秀!