JavaScript Promise, async/await, and Event Loop Traces
Six traces emphasizing microtask vs macrotask ordering, `Promise.all` timing, `await`-as-microtask sequencing, and the classic `var` + `setTimeout` loop pitfall.
Question Bank
Hard
JavaScript
quiz
js-event-loop
promises
async-await
832 views
17
What does the following code print, and how would you fix it so the output is 0, 1, 2, 3?
Examples
Example 1:
Input: var loop in 4 setTimeouts (0ms)
Output: 4 4 4 4
Explanation: The loop runs to completion before the event loop fires any callback, so all four closures read the same shared var i which is now 4.5 more questions, with full solutions and explanations, are available for premium members.
Upgrade to Premium