Sobes.tech
Middle

console.log('1'); setTimeout(() => console.log('2'), 0); Promise.resolve().then(() => console.log('3')); console.log('4');

sobes.tech AI

Answer from AI

The output will be:

1
4
3
2

Explanation:

  • console.log('1') — synchronous call, outputs 1.
  • setTimeout(..., 0) — places the function in the macro task queue, executed after the current stack and microtasks.
  • Promise.resolve().then(...) — places the function in the micro task queue, which executes immediately after the current stack but before macro tasks.
  • console.log('4') — synchronous call, outputs 4.

Execution order:

  1. Synchronous code: 1, 4
  2. Microtasks: 3
  3. Macrotasks: 2