- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Node.js 文档的以下段落感到困惑。
setImmediate()
vssetTimeout()
... The order in which the timers are executed will vary depending on the context in which they are called. If both are called from within the main module, then timing will be bound by the performance of the process (which can be impacted by other applications running on the machine).
For example, if we run the following script which is not within an I/O cycle (i.e. the main module), the order in which the two timers are executed is non-deterministic, as it is bound by the performance of the process:
它继续显示以下示例
// timeout_vs_immediate.js
setTimeout(() => {
console.log('timeout');
}, 0);
setImmediate(() => {
console.log('immediate');
});
$ node timeout_vs_immediate.js
timeout
immediate
$ node timeout_vs_immediate.js
immediate
timeout
我不明白是什么导致结果不确定。由于 timers
阶段发生在 check
阶段之前,由 setTimeout
安排的回调不应该总是在由 setImmediate< 安排的回调之前执行吗
?我不认为事件循环中的阶段顺序会因上下文切换或其他原因而改变。
文档还指出
However, if you move the two calls within an I/O cycle, the immediate callback is always executed first:
好的,但是什么使所谓的“I/O 周期”与主模块不同?
我知道有很多相关问题,但所有答案都只是通过引用文档来说明这一事实,而没有解释非确定性在何处发挥作用,因此我认为这不是重复。
最佳答案
真正的技巧在 Timeout constructor 中这是由 setTimeout
调用的,它将低于 1 的时间增加到 1。因此 setTimeout(fn, 0)
实际上等同于 setTimeout(fn, 1)
.
当 libuv initializes ,它在更新其内部时钟后从计时器开始,当一毫秒已经过去时,它会在进入轮询阶段(随后是 setImmediate 阶段)之前拾取计时器。
另一个有趣的观察是,多个计时器也可能在 setImmeadiate 之前 和 之前运行:
setTimeout(() => console.log('timer'), 1);
setTimeout(() => console.log('timer'), 1);
setImmediate(() => console.log('immediate'));
// can produce:
// timer
// immediate
// timer
那是因为setTimeout
在内部调用了getLibuvNow
,会调用env->GetNow()
,不仅获取了libuv的当前时间, 但是 also updates it .因此,定时器可能会以不同的到期时间被放入定时器队列,因此定时器阶段只会选择其中的一些。
OK, but what makes so-called "I/O cycles" different from the main module?
主模块在 libuv 初始化之前运行,而大多数其他代码将在 libuv loop 的 poll 阶段运行。 .因此,主模块初始化之后是计时器阶段,而轮询阶段之后是“检查句柄”阶段,其中运行 setImmediate
回调。因此通常在主模块中,定时器会在立即数之前运行(如果它们到期的话),如果在回调中安排,立即数会在定时器之前运行。
关于javascript - `setImmediate()` 和 `setTimeout()` 的计时如何受进程性能的约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72834122/
接下来是 TypeScript 中的 Jest 测试。我想知道为什么需要 setImmediate() 。 第一个例子是一个有效的测试。接下来是我尝试过但不起作用的各种事情。我不明白发生了什么。 pu
下面的代码有效 - 但在我将 NODEJS 从 4.. 更新到 6.9.1 后,它突然抛出一个错误(对于每个调用)...这意味着它完美地执行了所有递归调用,然后告诉我大约 20 倍的错误下面的消息..
我正在尝试在我的 NodeJS 环境中创建一个每秒执行 30 次的循环(基于固定变量)。我被告知 setInterval 和 setTimeout 不是像 process.nextTick 和 set
据我了解,process.nextTick() 和 setImmediate() 之间的区别如下: 由 process.nextTick() 调度的回调将在进入下一个事件循环之前全部执行,而由 set
Node.js 版本 0.10 今天发布,并引入了 setImmediate。 API changes文档建议在执行递归 nextTick 调用时使用它。 来自什么MDN says它看起来与proce
Salam(意思是你好:)) 在我的 node.js 应用程序中,我需要使用 setImmediate() 来隐性调用函数并保持其上下文完整以供下次执行。 考虑以下示例: var i=3; funct
Node.js 0.10 版今天发布,引入了setImmediate。 API changes文档建议在进行递归 nextTick 调用时使用它。 来自MDN says它似乎与 process.nex
我对 Node.js 文档的以下段落感到困惑。 setImmediate() vs setTimeout() ... The order in which the timers are execute
我想异步调用给定的函数。包装函数 tryCallAsync 是执行此操作的一种方法。这种方法有效。但是,它要求 setImmediate 的回调是异步函数。这似乎是错误的,因为回调正在返回一个未使用的
使用 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/EventLoop 中的视觉表示: setImmediate() 和
在我的情况下,方法必须通过 setImmediate 递归和异步地调用自身。 下一个代码与原始代码没有任何关系,但出现了同样的“问题”:“this”引用似乎在第二次调用时丢失了。 function A
目前有 2 个不同的 API 旨在将任务分解为预定的回调函数。 setImmediate (非标准) requestIdleCallback (实验性的) 据我所知,目前还没有实现这两种功能的 Jav
我想知道如何将 setImmediate 与 async wait 结合使用并正确处理错误。我写了以下代码。但我不确定它是否遵循最佳实践。 我的 Express 应用程序中有一条路线 router.g
我正在学习 setImmediate() 的工作原理,但遇到了一个我找不到技术解释的怪事。 代码非常简单: setImmediate(function(){ console.log("thir
在 nodejs 的官方网站(https://nodejs.org/api/timers.html#timers_setimmediate_callback_arg)中,据说: setImmediat
我在 Node documentation 上读到了这个: setImmediate(callback, [arg], [...]) To schedule the "immediate" execu
Onsen UI 在动态渲染温泉的 js 和 css 时抛出错误。但是当我在静态 html 中使用它时它正在工作。错误: Uncaught ReferenceError :未定义 setImmedia
我正在使用 fast-csv ( https://www.npmjs.com/package/fast-csv ) 来解析 csv 文件。 它可能有 10k 条记录,因此需要花费大量时间来解析,并且该
据我了解,对于 setImmediate 的名称存在一些争议。有谁知道这个有用的功能是否会很快进入 V8/Spider Monkey,无论名称如何? 最佳答案 https://developer.mo
nodejs文档说 在 I/O 事件的回调之后和由 setTimeout 和 setInterval 设置的计时器被触发之前,安排“立即”执行回调。返回可能与 clearImmediate 一起使用的
我是一名优秀的程序员,十分优秀!