Asynchronous JavaScript and the event loop
Learning the event loop, task and microtask queues, Promise combinators and cancellation patterns, you correctly handle asynchronous code that waits, races and fails.
Level 301 · 16 lessons · 15 practice labs
Lessons
The event loop
- The call stack and the single thread
- Tasks and microtasks
- The phases of the Node event loop
- Recognising blocking code
- Short task: predict the output order
Promises and async
- From callbacks to promises
- Promise chains and error propagation
- Promise.all, allSettled, race and any
- for await and async iterators
- Cancellation with AbortController
- Unhandled rejections and global handlers
- Short task: bulk downloads with limited concurrency
Patterns
- Retry and exponential backoff
- Job queues and concurrency limits
- Moving heavy work to a worker thread
- Capstone: a resilient data collector
Practice labs
- Find the code that freezes the server: Find the code blocking the event loop in a Node server that stops answering while a report is built.
- Show microtask starvation: Measure how a self-requeuing promise chain starves timers, then fix it.
- Chunk a heavy loop: Split a loop over a hundred thousand records into chunks with setImmediate to keep the server responsive.
- Flatten nested callbacks: Turn a three-level callback file-reading routine into a flat flow with async/await.
- Find the swallowed error: An error goes uncaught and the result is undefined because a return is missing in a chain; find and fix it.
- Track down an unhandled rejection: Add a global rejection handler to a worker process that crashes silently now and then, and find the source.
- Run three requests in parallel: Parallelise three independent service calls that wait one after another with Promise.all and time it.
- Don't lose the report when one branch fails: While collecting five branches' stock reports, still report the others with allSettled when one fails.
- Pick the fastest mirror: Take the first successful answer from three mirror servers with any and cancel the rest with AbortController.
- Cancel a long download: Write a function that stops a running download with AbortController when the user gives up.
- Walk a paged API with for await: Walk the lab's paged orders service with an async generator and process every record in one stream.
- Pass the cancel signal through every layer: Make a search that calls three nested async functions stop completely when it is cancelled.
- Retry on a transient error: Write a simple three-attempt retry wrapper for a service that sometimes returns 503.
- Add jitter to exponential backoff: Stop a hundred clients retrying at once from flooding a service with jittered exponential backoff.
- Move hashing to a worker thread: Move a slow endpoint that computes password hashes to worker_threads and compare other requests' latency.
About this course
Learning the event loop, task and microtask queues, Promise combinators and cancellation patterns, you correctly handle asynchronous code that waits, races and fails.
What you will learn
- The event loop
- Promises and async
- Patterns
Prerequisites
You should be comfortable with the level 101 and 201 topics of the same path.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 301; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.