Q14 [Coding]: Predict the execution output of this asynchronous code snippet.
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
Output: 1, 4, 3, 2.
Reason: Synchronous logs (1, 4) run first. Promise.then enters the Microtask queue (3), executing before setTimeout in the Macrotask queue (2).
Key Takeaways
- Synchronous code runs to completion first.
- Microtasks (
Promise) run before Macrotasks (setTimeout).
Knowledge Check
In what order are the microtask and macrotask logged above?