CodePark/ questions / Event loop
START PRACTICINGPRACTICE
Frontend question · JavaScript › Event loop

What does this log?

console.log("A");
new Promise((resolve) => {
  console.log("B");
  resolve();
  console.log("C");
}).then(() => console.log("D"));
console.log("E");
A B C E D

The answer is A B C E D. The promise executor (the function passed to new Promise) runs synchronously, right away. So B and C log immediately. Calling resolve() does not stop the executor. Only the then callback is queued as a microtask. It runs after the synchronous E.

The tempting option "A B E C D" is wrong because it assumes
resolve() pauses the executor. It does not, so C logs before E.