Terminology
- CallStack
- Task queue
- Promise
Execution Order
I had a misunderstanding about the execution order, so I'm summarizing it:
-
orderExecutoris called and enters the Call Stack. -
orderExecutorexits the Call Stack. -
thenenters 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,
thenmoves from the Task Queue into the Call Stack. -
thenis 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)