Event Loop and Task Scheduling
- One thread. JavaScript runs your code on one thread: only one statement executes at a time, so nothing may block for long.
- The event loop. The event loop repeatedly takes the next finished task: a timer, a click, an answer: and runs its callback.
- Microtasks. Promise callbacks are microtasks, which run before the next timer or click: the queue with priority.
Promises and async/await
- Promises. A promise is an object standing for a value that is still coming: pending now, fulfilled or rejected later.
- async/await. Inside an async function, await pauses that function until a promise settles, letting async code read top to bottom.
- Rejections. A rejected promise skips to the nearest catch: an unhandled rejection is the async version of an uncaught throw.
Concurrent Asynchronous Work
- Sequential versus parallel. Awaiting calls one by one is sequential; starting them together and awaiting Promise.all runs them concurrently.
- Racing and timeouts. Promise.race settles with whichever promise finishes first: the standard trick for adding a timeout to slow work.
- Cancellation. An AbortController carries a signal into async work so a request stops when the user has moved on.
Before and after this level
- Before: Level 8: Errors & Debugging: Exceptions and Error Handling, Debuggers and Stack Traces, Defensive JavaScript.
- Next: Level 10: Browser JavaScript: DOM and Events, Forms and User Interaction, Browser APIs and Storage.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does JavaScript Level 9: Asynchronous JavaScript cover?
Event Loop and Task Scheduling, Promises and async/await, Concurrent Asynchronous Work: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 8: Errors & Debugging, which covers Exceptions and Error Handling, Debuggers and Stack Traces, Defensive JavaScript.