NodeJS 0.11 as well as io.js and the Node 0.12 branch all ship with native promises.
Native promises have a .then
method which always executes on a future event loop cycle.
So far I've been using setImmediate
to queue things to the next iteration of the event loop ever since I switched from nextTick:
setImmediate(deferThisToNextTick); // My NodeJS 0.10 code
process.nextTick(deferThisToNextTick); // My NodeJS 0.8 code
Since we now have a new way to do this:
Promise.resolve().then(deferThisToNextTick);
Which should I use? Also - does Promise.resolve.then
act like setImmediate
or like nextTick
with regards to code running before or after the event loop?