0

Can anyone please explain why the result of the below code is - 1,2,3,4,5,6? Does this imply that normal code within the promise body executes synchronously? I was under the impression that the result of the promise depends on how it is handled by the .then() block, i.e., the promise body code is executed asynchronously.

console.log(1);

const promise = new Promise((resolve) => {
    console.log(2)
    resolve()
    console.log(3)
});
    
console.log(4)
    
promise.then(() => {
    console.log(5)
}).then(() => {
    console.log(6)
})
Usitha Indeewara
  • 870
  • 3
  • 10
  • 21
Ankit_M
  • 276
  • 1
  • 3
  • 15
  • 2
    Yes, the *promise executor function* is always executed synchronously. Its purpose is to begin the process that will report its completion via the promise. *(Promises don't have a "body." They aren't functions. They're simply objects used to provide uniform semantics for observing the completion of asynchronous processes.)* – T.J. Crowder Oct 30 '22 at 10:22

0 Answers0