【发布时间】:2014-04-24 12:39:09
【问题描述】:
我在节点documentation 上读到了这篇文章:
setImmediate(callback, [arg], [...])
安排在 I/O 事件回调之后和
之前“立即”执行回调setTimeout和setInterval
但是,我看到了相反的情况。
setTimeout 在 setImmediate 之前执行。
是否有人对此行为有解释,或有关节点事件循环的任何文档?
谢谢:)
代码:
var index = 0;
function test(name) {
console.log((index++) + " " + name);
}
setImmediate(function() {
test("setImmediate");
})
setTimeout(function() {
test("setTimeout");
}, 0);
process.nextTick(function() {
test("nextTick");
})
test("directCall");
输出:
0 directCall
1 nextTick
2 setTimeout
3 setImmediate
【问题讨论】:
-
当我运行你的代码时(在节点 v0.10.31 上)我得到:0 directCall 1 nextTick 2 setImmediate 3 setTimeout
标签: javascript node.js