DEV Community

daichitt
daichitt

Posted on

Promise JavaScript

Terminology

  • CallStack
  • Task queue
  • Promise

Execution Order

I had a misunderstanding about the execution order, so I'm summarizing it:

  1. orderExecutor is called and enters the Call Stack.
  2. orderExecutor exits the Call Stack.
  3. then enters the Task Queue.
  4. console.log('2') is called and enters the Call Stack.
  5. console.log('2') exits the Call Stack.
  6. Since the Call Stack is empty, then moves from the Task Queue into the Call Stack.
  7. 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
Enter fullscreen mode Exit fullscreen mode

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

https://www.youtube.com/watch?v=eiC58R16hb8&t=518s

Top comments (0)