Skip to content
Onsite

Output: puzzle mixing async functions, nested awaits, then chains, queueMicrotask and setTimeout

HardP2Output

What does this code print? Track the call stack, the microtask queue and the timer queue.

1async function a1() {
2 console.log('a1 start');
3 await a2();
4 console.log('a1 end');
5}
6async function a2() {
7 console.log('a2');
8}
9console.log('script start');
10setTimeout(() => console.log('timeout'), 0);
11a1();
12new Promise((resolve) => {
13 console.log('p1');
14 resolve();
15})
16 .then(() => console.log('then1'))
17 .then(() => console.log('then2'));
18queueMicrotask(() => console.log('micro'));
19console.log('script end');
Saved automatically

One line per console.log. Arrays as [1, 2, 3], objects as { a: 1 }, an uncaught error as Uncaught TypeError: message (Chrome and Node wording).

Hints

Saved automatically