Sobes.tech
Middle

What will be the output of the following code snippet? ```javascript setTimeout(() => { console.log('B') }, 0) Promise.resolve().then(() => { console.log('C') }) Promise.resolve().then(() => { console.log('D') }) console.log('E') ```

sobes.tech AI

Answer from AI

The output will be:

E
C
D
B

Explanation:

  • console.log('E') executes synchronously immediately.
  • Promise.resolve().then(...) adds callbacks to the microtask queue, which execute after the current stack.
  • setTimeout(..., 0) adds a callback to the macrotask queue, which executes after microtasks.

Order of execution:

  1. Synchronous code: outputs E.
  2. Microtasks: outputs C and D (in order of addition).
  3. Macrotasks: outputs B.

Therefore, the final order is: E, C, D, B.

What will be the output of the following code… - sobes.tech