1

I'm a little confused about the order in which Promise executes in eventloop.

setsetImmediate(function () {
     console.log("setImmediate");
});

Promise.resolve().then(function (value) {
    console.log("Promise");
});
process.nextTick(function () {
    console.log("process.nextTick()")
});
console.log("Starting... ...");
//Starting... ...
//process.nextTick()
//Promise
//setImmediate

but if I require a bluebird,it's result will change.

var Promise = require("bluebird");
...

//Starting... ...
//process.nextTick()
//setImmediate
//Promise

Why is it so?

box_ch
  • 21
  • 2
  • Typo: `setsetImmediate` line 1 in the first code block. – momocow May 04 '18 at 02:22
  • What you are seeing is indeterminate behavior that is not described in the promise specification. If you require a specific ordering, then you should code it to generate a specific ordering, not rely on implementation details that are not in the promise specification. And, yes different promise implementations have different implementation details. – jfriend00 May 04 '18 at 02:45
  • 1
    I believe when implementing the promises bluebird had to use the available event loop's macrotask queue which is the only available option to them. However native promises use the microtask queue which is a separate queue for event loop to visit after a single macrotask is executed. Accessing to the microtask queue is made available to native promises by the JS engine so native promises can insert the tasks into the microtask queue while bluebird has to use the macrotask queue like `setTimeout()` etc... [More here](https://abc.danch.me/microtasks-macrotasks-more-on-the-event-loop-881557d7af6f) – Redu May 04 '18 at 10:13

0 Answers0