Priority between setTimeout and setImmediate

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 
+8
javascript
source share
1 answer

You should check this github issue

The event loop cycle is the timers → I / O → which immediately rinses and repeats. The documentation is correct, but incomplete: it does not mention that when you have not yet entered the event loop (as in your case, an example), then timers are in the first place - but only in the first tick. (To the wizard. To complicate things, everything works a little less deterministic in v0.10.)

+3
source share

All Articles