看了《深入浅出Node.js》,书中第六十三页有关于setImmediate和process.nextTick的区别,书上的意思是: setImmediate设置的回调函数以链表形式存储,每个事件循环周期只执行一个回调; process.nextTick设置的回调以数组方式存储,在下一次循环中按顺序执行数组中所有回调; process.nextTick设置的回调先于setImmediate执行。 所以下面的代码结果应该是:
normal execution nextTick 1 nextTick 2 setImmediate 1 get in setImmediate 2
但是我在自己电脑上执行结果是:
normal execution nextTick 1 nextTick 2 setImmediate 1 setImmediate 2 get in
我的node是4.2.1的,请问是setImmediate的实现改变了么?
代码: process.nextTick(function(){ console.log(‘nextTick 1’); }); process.nextTick(function(){ console.log(‘nextTick 2’); });
setImmediate(function(){ console.log(‘setImmediate 1’); process.nextTick(function(){ console.log(‘get in’); }); }); setImmediate(function(){ console.log(‘setImmediate 2’); }); console.log(‘normal execution’);
书上说得很清楚啊。