Promise.resolve().then vs setImmediate vs nextTick

前端 未结 3 1961
旧时难觅i
旧时难觅i 2020-12-01 04:36

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

3条回答
  •  无人及你
    2020-12-01 05:02

    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.

提交回复
热议问题