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:
- Synchronous code: outputs
E. - Microtasks: outputs
CandD(in order of addition). - Macrotasks: outputs
B.
Therefore, the final order is: E, C, D, B.