How are node Promises getting in between `nextTick` and `setImmediate`?

穿精又带淫゛_ 提交于 2019-12-19 03:45:56

问题


I had a weird timing bug in my app that came from switching from Bluebird to native promises. I fixed it, but am left with this oddity: Native promises seem to wiggle their way in between nextTick and setImmediate -- how? And is this supposed to happen? Where are promises supposed to go with regard to these?

~function(){
  setTimeout            (console.log.bind(console, 'timeout A'));
  process.nextTick      (console.log.bind(console, 'nextTick A'));
  setImmediate          (console.log.bind(console, 'setImmediate A'));
  Promise.resolve().then(console.log.bind(console, 'promise'));
  process.nextTick      (console.log.bind(console, 'nextTick B'));
  setImmediate          (console.log.bind(console, 'setImmediate B'));
  setTimeout            (console.log.bind(console, 'timeout B'));
}();

Native yields:

nextTick A
nextTick B
promise undefined
setImmediate A
setImmediate B
timeout A
timeout B

Bluebird yields:

nextTick A
nextTick B
setImmediate A
promise undefined
setImmediate B
timeout A
timeout B

回答1:


Native promises seem to wiggle their way in between nextTick and setImmediate -- how? And is this supposed to happen? Where are promises supposed to go with regard to these?

Yes, promises run after the microtask nextTick queue and before any tasks (like setImmediate) execute.

This is their intended behavior, and what we expect them to do in NodeJS. This was decided here and you can read about it here

Bluebird's different behavior

Bluebird's behavior predates native promises, bluebird 3.0 uses nextTick and microtask semantics for scheduling. Bluebird lets you manually override this behavior using Promise.setScheduler with nextTick as the scheduler (instead of setImmediate).

You can see the code here:

GlobalSetImmediate.call(global, fn)

NOTE THAT YOUR CODE SHOULD NOT RELY ON THESE BEHAVIORS ANYWAY.



来源:https://stackoverflow.com/questions/32453877/how-are-node-promises-getting-in-between-nexttick-and-setimmediate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!