I am curious about the relationship between Event Loop and Promise.
The demo exposes the question. I expected the p1 fulfilled
appear in the middle, since they queue a task to the same task queue and are executed one by one.
var p1 = new Promise(function(resolve, reject){
resolve(1)
})
setTimeout(function(){
console.log("will be executed at the top of the next Event Loop")
},0)
p1.then(function(value){
console.log("p1 fulfilled")
})
setTimeout(function(){
console.log("will be executed at the bottom of the next Event Loop")
},0)
The console result is:
p1 fulfilled
will be executed at the top of the next Event Loop
will be executed at the bottom of the next Event Loop
The visualized effect shows the promise.then
's callback didn't go to the task queue of the Event Loop. It's right?
【NOTE: The question is not the same as Promise vs setTimeout, since it focus more on the relationship between Event Loop and Promise】
The promise in the last section has fulfilled with a value, but you also want to be able to access the value. Promises have a method called then that will run after a promise reaches resolve in the code. then will return the promise's value as a parameter.
JavaScript has a runtime model based on an event loop, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks. This model is quite different from models in other languages like C and Java.
The event loop is actually composed of one or more event queues. In each queue, events are handled in a FIFO order. It's up to the browser to decide how many queues to have and what form of prioritisation to give them. There's no Javascript interface to individual event queues or to send events to a particular queue.
That is where the event loop comes into picture, it takes the first event from the Event Queue and places it onto the stack i.e in this case the callback function. From here, this function executes calling other functions inside it, if any. This cycle is called the event loop and this how JavaScript manages its events.
Each event loop has a microtask queue and a macrotask queue.
A microtask is a task that is originally to be queued on the microtask queue rather than a task queue. Refer to https://www.w3.org/TR/html51/webappapis.html#microtask-queue.
There are two kinds of microtasks:
Promise
,Object.observe
, MutationObserver
and process.nextTick
in Node.js.And the macrotask queue mainly contains setTimeout
, setInterval
, setImmediate
, requestAnimationFrame
, I/O
in Nodejs.
In a event Loop, these two task queues will run in two steps:
In your example:
new Promise
and resolve
are synchronous;setTimeout
macroTask into the macrotask queue;promise.then(function(){})
to the microtask queue, this task will run immediately, because the Promise initialize and resolve are synchronous, this task run before any macrotask; so, console.log the p1 fulfilled
;setTimeout
to macrotask queue;for this code:
setTimeout(function(){ console.log("will be executed at the top of the next Event Loop") },0) var p1 = new Promise(function(resolve, reject){ setTimeout(function(){resolve(1)},0) }); setTimeout(function(){ console.log("will be executed at the bottom of the next Event Loop") },0) for (var i = 0; i < 100; i++) { (function(j){ p1.then(function(value){ console.log("promise then - " + j) }); })(i) }
the output order:
will be executed at the top of the next Event Loop promise then - 0 promise then - 1 promise then - 2 ... promise then - 99 will be executed at the bottom of the next Event Loop
setTimeout
to macrotask queue, and a microtask promise.then()
to the microtask queue;If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With