The difference between the execution order of bluebird and native Promise for then

when using bluebird and nodejs native Promise to execute the following code, the order of execution is somewhat different. What is the reason for this?

/**
 * bluebird, 2 3 5 6 1 4
 * Promise 2 3 5 6 4 1
 */
// var Promise = require("bluebird");

setTimeout(() => {
    console.log("1");
}, 0);

process.nextTick(function() {
    console.log("6");
});

new Promise(function(resolve, reject) {
    console.log("2");
    var st = new Date().getTime();
    while(new Date().getTime() - st < 1000) {}
    resolve("ok");
    console.log("3");
})
.then(function() {
    console.log("4");
});

console.log("5");

question thinking:

1. Native Promise processes then in the micro task queue, so it takes precedence over macro task (setTimeout).
2. What is the handling of then in bluebird?

Sep.05,2021

guess:
for native Promise, is a micro-task operation, the implementation of giving priority to
bluebird is quite different from the native Promise mechanism. It seems that callback functions are added through .then, and even functions that are added after the asynchronous operation is completed will be called, so then will be placed in macro task processing.


after the execution of the initial macro task, the micro task will be cleared in one breath.
the native Promise must be a microtask, so it is 2jin3jin5, (switching to the microtask queue) 6pd4, (the microtask is emptied and back to the macro task setTimeout) 1.

bluebird's Promise is not native, so you can only use setTimeout to simulate asynchronism (not native, so you can't operate the underlying macro / micro task queue, and you can't decide whether your task belongs to macro or micro task. If you try to implement a Promise, yourself, you can only simulate it with setTimeout, and the output order should be the same as that of bluebird, and I haven't tried . In the case of
bluebird: 2meme 3jol 5, (go to the micro task queue, only one native process.nextTick is a micro task) 6, (the micro task is emptied and then return to the macro task, both are setTimeout) 1meme 4.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b308c8-2bd09.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b308c8-2bd09.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?