0
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)
})
console.log(7);

Output for this block is 1,2,3,4,7,5,6

I do understand that promise callback will go event loop and will run once promise is resolved then how we are getting 2, 3 printed before the promise is resolved. can someone please explain this to me?

K.Kaur
  • 61
  • 3
  • 7
  • 1
    The promise executor function (the function you pass `new Promsie`) is run **synchronously** when you call `new Promise`, so it can start the operation that the promise will report the completion of. `resolve` isn't a blocking call, it just (in this case) fulfills the promise. It's the promise fulfillment/rejection *callbacks* that will be asynchronous, not the call to the executor function. Other than those callbacks, promises don't make anything asynchronous that isn't already asynchronous. Your executor function code isn't, so it remains entirely synchronous. – T.J. Crowder Feb 08 '22 at 10:53

0 Answers0