nextTick vs set立即,直观的说明 [英] nextTick vs setImmediate, visual explanation

查看:66
本文介绍了nextTick vs set立即,直观的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对nextTick和setImmediate之间的区别感到非常困惑.我已经在互联网上阅读了有关它们的所有文档,但我仍然不了解它们的工作原理.

I'm very confused about the differences between nextTick and setImmediate. I've read all the documentation about them on the internet but I still don't understand how they work.

示例:

function log(n) { console.log(n); }

立即设置

setImmediate(function() {
  setImmediate(function() {
    log(1);
    setImmediate(function() { log(2); });
    setImmediate(function() { log(3); });
  });
  setImmediate(function() {
    log(4);
    setImmediate(function() { log(5); });
    setImmediate(function() { log(6); });
  });
});

//1 2 3 4 5 6

nextTick

process.nextTick(function() {
  process.nextTick(function() {
    log(1);
    process.nextTick(function() { log(2); });
    process.nextTick(function() { log(3); });
  });
  process.nextTick(function() {
    log(4);
    process.nextTick(function() { log(5); });
    process.nextTick(function() { log(6); });
  });
});

//1 4 2 3 5 6

为什么会有这些结果?请以视觉或非常容易理解的方式进行解释.甚至节点核心开发人员也不同意人们应该如何理解nextTick和setImmediate.

Why these results? Please explain with a visual or very easy to follow explanation. Even the node core devs don't agree at how nextTick and setImmediate should be understood by people.

来源:

  • setImmediate vs. nextTick
  • Why is setImmediate much more slower than nextTick?
  • setImmediate is not always very immediate

推荐答案

请考虑以下两个示例:

立即设置

setImmediate(function A() {
  setImmediate(function B() {
    log(1);
    setImmediate(function D() { log(2); });
    setImmediate(function E() { log(3); });
  });
  setImmediate(function C() {
    log(4);
    setImmediate(function F() { log(5); });
    setImmediate(function G() { log(6); });
  });
});

setTimeout(function timeout() {
  console.log('TIMEOUT FIRED');
}, 0)

// 'TIMEOUT FIRED' 1 4 2 3 5 6
// OR
// 1 'TIMEOUT FIRED' 4 2 3 5 6

nextTick

process.nextTick(function A() {
  process.nextTick(function B() {
    log(1);
    process.nextTick(function D() { log(2); });
    process.nextTick(function E() { log(3); });
  });
  process.nextTick(function C() {
    log(4);
    process.nextTick(function F() { log(5); });
    process.nextTick(function G() { log(6); });
  });
});

setTimeout(function timeout() {
  console.log('TIMEOUT FIRED');
}, 0)

// 1 4 2 3 5 6 'TIMEOUT FIRED'

每次迭代一次,都会按顺序将

setImmediate回调从事件循环中触发.因此,在事件循环的第一次迭代中,将触发回调A.然后,在事件循环的第二次迭代中,将触发回调B,然后在事件循环的第三次迭代中,将触发回调C,以此类推.这样可以防止事件循环被阻塞,并允许其他I/O或计时器回调在平均时间内调用(如0ms超时的情况,它在第1次或第2次循环迭代中触发).

setImmediate callbacks are fired off the event loop, once per iteration in the order that they were queued. So on the first iteration of the event loop, callback A is fired. Then on the second iteration of the event loop, callback B is fired, then on the third iteration of the event loop callback C is fired, etc. This prevents the event loop from being blocked and allows other I/O or timer callbacks to be called in the mean time (as is the case of the 0ms timeout, which is fired on the 1st or 2nd loop iteration).

nextTick回调总是在执行完当前代码后立即返回事件循环之前立即触发.在nextTick示例中,我们最终将执行所有nextTick回调,然后再返回事件循环.由于将从事件循环中调用setTimeout的回调,因此直到完成每个nextTick回调后,才会输出文本"TIMEOUT FIRED".

nextTick callbacks, however, are always fired immediately after the current code is done executing and BEFORE going back to the event loop. In the nextTick example, we end up executing all the nextTick callbacks before ever returning to the event loop. Since setTimeout's callback will be called from the event loop, the text 'TIMEOUT FIRED' will not be output until we're done with every nextTick callback.

这篇关于nextTick vs set立即,直观的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆