JavaScript, inside out · Chapter 41 of 51
The event loop
By the end of this chapter you can say in which order JavaScript runs ordinary code, promise callbacks and timers, and use that order on purpose. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Going async
- 5 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
On the order screen, the "Paid" badge sometimes appears before the receipt line and sometimes after, depending on whether the receipt came from a promise or a timer. Ravi thinks the code is haunted. And when the screen prints a long list of kitchen tickets, every tap on it is ignored until the printing is done. Kabir says both are perfectly predictable once you know how JavaScript picks what runs next, and that interviewers love asking exactly this.
So far you have read code from top to bottom. Since chapter 39, some of it runs later: a then callback, the rest of an async function, a timer. "Later" is not good enough when the order of two lines on the screen matters.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
basicOrder: the receipt, the promise and the timer
Export basicOrder, an array of strings: every line snippet 1 logs, in the order they are logged. Use exactly the text of each log call.
The check runs the same snippet and compares.
- Task 2
kitchenTickets: microtasks that queue microtasks, and timers
Export kitchenTickets, an array of strings: every line snippet 2 logs, in the order they are logged. Use exactly the text of each log call.
The check runs the same snippet and compares.
- Task 3 · interview question
Interview: classicQuiz, async, await and a promise executor
Export classicQuiz, an array of strings: every line snippet 3 logs, in the order they are logged. Use exactly the text of each log call.
The check runs the same snippet and compares.
- Task 4
afterThisTick: after the current code, before any timer
Write afterThisTick(fn). Arrange for fn to run once the code that is running now has finished, but before any timer that is already waiting. Do not call fn straight away, and return nothing.
With a timer of 0 ms set first, afterThisTick(() => log('tick')) and then log('now'), the lines come out now, tick, timer.
- Task 5
processInChunks: a long list in chunks, the screen stays responsive
Write the async function processInChunks(items, handle, size). Call handle(item) for every item, in order, size items at a time. The first chunk is handled straight away, before processInChunks returns its promise. Before each later chunk, wait for a promise that a setTimeout of 0 ms resolves, so timers and clicks waiting in the task queue get a turn between chunks.
Resolve with nothing once every item is handled. A list shorter than size is handled in one go, and an empty list resolves with undefined.
What you will learn
- The call stack: the code running right now, which always runs to the end.
- Two queues: microtasks (promise callbacks, await, queueMicrotask) and tasks (timers, clicks).
- The loop's rule: when the stack is empty, every microtask first, then one task.
- Using the queues on purpose: run something just after the current code, or give way so clicks get a turn.
More in Going async
Build The event loop yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.