Promise.resolve().then vs setImmediate vs nextTick

13,641

Solution 1

Using Promise.resolve().then has no advantages over nextTick. It runs on the same queue, but have slightly higher priority, that is, promise handler can prevent next tick callback from ever running, the opposite is not possible. This behaviour is an implementation detail and should not be relied on.

Promise.resolve().then is obviously slower (a lot, I think), because it creates two promises which will be thrown away.

You can find extensive implementation info here: https://github.com/joyent/node/pull/8325

The most important part: Promise.resolve().then is like nextTick and not like setImmediate. Using it n place of setImmediate can change your code behaviour drastically.

Solution 2

I'm not going to answer the bolded part about technicalities, but only the question

Which should I use?

I don't think there is any reason to use Promise.resolve().then() unless you are interested in the promise for the result of your asynchronously executed function. Of course, if you are, then this would be far superior than dealing with callback hell or making a new Promise from setTimeout or nextTick.

There's also a second technical difference, more import than the timing: promises do swallow exceptions. Which you probably don't want. So, like @vkurchatkin mentioned, don't create promises only to throw them away. Not only because it's slower, but because it makes your code less readable and your app more error-prone.

Share:
13,641
Benjamin Gruenbaum
Author by

Benjamin Gruenbaum

Hi, I'm Benjamin 👋 You can find me here or if you need to reach me. At my day job I work at Microsoft on JavaScript development infrastructure in WiCD. Before that I was in Testim.io working on automating test automation and empowering QA developers Before that I worked on distributed algorithms and platforms for the webrtc based Peer5 P2P CDN, before that I wrote ran the core dev team at TipRanks writing csharp and javascript. I also do a bit of open source, here are some projects I am involved with: Node.js node.js platform - core collaborator. Bluebird bluebird core team member and maintainer. MobX mobx core team. Sinon.JS sinon.js team member. If you want to get involved in Node.js (at any capacity, no experience required) please do reach out. I promise you don't need perfect English, l33t coding skills or to be a "bro" to fit in (but those are welcome too). My email is written in the node home page. If you have an interesting use case for async-iterators/generators in Node.js - we're interested in talking in particular :) I have gold tags in promise javascript and a few others because I've spent a year answering as many promises questions as I could back then. If you're building something cool with promises please don't hesitate to reach out. I hereby release any code, test or multimedia content written in any answer and/or question by me in StackOverflow and anywhere else in the Stack Exchange network as public domain. No acknowledgement is required (although it is appreciated).

Updated on June 12, 2022

Comments

  • Benjamin Gruenbaum
    Benjamin Gruenbaum almost 2 years

    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?