The following code:
let promise = new Promise(function(resolve, reject) {
console.log("resolve, reject are", resolve, reject);
resolve("this is the success data");
console.log("Point A");
setTimeout(function() { console.log("Point B"); }, 0);
});
promise.then(
function(a) {
console.log("Success.", a);
setTimeout(function() { console.log("Point C"); }, 0);
promise.then(function(a) { console.log("Go have a drink.", a); });
},
function(a) {
console.log("failure", a)
}
);
console.log("Point D");
adds printing of "Point B" and "Point C" to the job queue, and after that, the handler that prints out "Go have a drink" is also added to the job queue. But "Go have a drink" is printed before "Point B" and "Point C" in the result in Node and in Google Chrome. Is it true that the promise has a separate job queue and the setTimeout
and setInterval
has a separate job queue, and the promise job queue is executed before the timed job queue? It seems the ES6 specs mentioned two job queues: ScriptJobs and PromiseJobs. Can we count on the promise job queue executed before the timed job queue?