Terminology
- CallStack
- Task queue
- Promise
Execution Order
I had a misunderstanding about the execution order, so I'm summarizing it:
-
orderExecutor
is called and enters the Call Stack. -
orderExecutor
exits the Call Stack. -
then
enters the Task Queue. -
console.log('2')
is called and enters the Call Stack. -
console.log('2')
exits the Call Stack. - Since the Call Stack is empty,
then
moves from the Task Queue into the Call Stack. -
then
is executed.
function orderExecutor(resolve, reject) {
console.log('1'); // 1
resolve('3');
}
let orderPizza = new Promise(orderExecutor);
orderPizza.then(function (value) {
console.log(value); // 3
});
console.log('2'); // 2
// 1, 2, 3
Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises
https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide/In_depth
Top comments (0)