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('nextTick powerful');
});
});
setImmediate(function() {
console.log('setImmediate 2');
});
setTimeout(function() {
console.log('timeout');
}, 0);
console.log('console');
按照朴大神的说法,nextTick 是保存在数组里,一次执行所有(没超出那个最大限制),setImmediate 是保存在链表,一次执行一个 官方文档也有描述:To schedule the “immediate” execution of callback after I/O events callbacks and before setTimeout and setInterval . 所以感觉上应该是这样: console nextTick 1 nextTick 2 setImmediate 1 nextTick powerful setImmediate 2 timeout
可node 为 v0.12.0这么执行的结果是这样的: console nextTick 1 nextTick 2 timeout setImmediate 1 setImmediate 2 nextTick powerful timeout 竟然在最前面,nextTick powerful 竟然在最后面
可node 为 v0.10.35这么执行的结果是这样的: console nextTick 1 nextTick 2 timeout setImmediate 1 nextTick powerful setImmediate 2 那个timeout还是不太符合预期。。。。
这,是我理解错了 ,还是node的机制更改了文档没告诉我么?
https://github.com/joyent/node/issues/6034#issuecomment-30019424
@tjfontaine: "timers are based on a time in the future, even if it’s 0, while check immediate is always on the next turn of the loop. So it’s possible that the delay of the event loop is low enough for the timer to fire after the immediate.
You shouldn’t necessarily be concerned about the order of operations in this regard though in my estimation."
https://github.com/joyent/node/blob/master/lib/timers.js#L200
exports.setTimeout = function(callback, after) {
var timer;
after *= 1; // coalesce to number or NaN
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
after = 1; // schedule on next tick, follows browser behaviour
}
timer = new Timeout(after);
if (arguments.length <= 2) {
timer._onTimeout = callback;
} else {
/*
* Sometimes setTimeout is called with arguments, EG
*
* setTimeout(callback, 2000, "hello", "world")
*
* If that's the case we need to call the callback with
* those args. The overhead of an extra closure is not
* desired in the normal case.
*/
var args = Array.prototype.slice.call(arguments, 2);
timer._onTimeout = function() {
callback.apply(timer, args);
}
}
if (process.domain) timer.domain = process.domain;
exports.active(timer);
return timer;
};