I read this in node documentation:
setImmediate (callback, [arg], [...])
Schedule an “immediate” callback after I / O event callbacks and before setTimeout and setInterval
However, I see the opposite. setTimeout runs before setImmediate . Does anyone have an explanation for this behavior or any documentation in the node event loop?
Thanks:)
the code:
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");
output:
0 directCall 1 nextTick 2 setTimeout 3 setImmediate
etnbrd
source share