JavaScript generator-style async
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Before async and await became common, many JavaScript codebases used generators plus Promise runners to write asynchronous flows that looked synchronous. That pattern is still useful when you need explicit execution control, custom scheduling, or legacy compatibility. This article explains generator-style async, shows a minimal runner, and compares it with modern async and await.
How Generator-Style Async Works
A generator function can pause at yield and resume later. If each yielded value is a Promise, a runner can wait for resolution and feed the result back into the generator.
The generator itself does not know how promises are resolved. It only describes the control flow.
Build a Minimal Generator Runner
A runner repeatedly calls next or throw based on Promise outcomes.
This is the core idea used by libraries that predated native async and await syntax.
Add Error Handling and Retries
Since control is centralized in the runner pattern, it is easy to add retry wrappers around yielded operations.
This pattern gives you composable resilience without deeply nested callbacks.
Compare with Async and Await
Async and await is easier to read for most applications and should be the default in modern runtimes. Generator-style async still has value when:
- You need to inspect or manipulate execution between each yield step.
- You want pluggable schedulers for testing.
- You maintain older code that already uses generator control flows.
If none of those apply, async and await usually reduces maintenance cost.
Testing Generator Flows
Generator-style async code is easiest to test when side effects are injected as dependencies. Pass an api object into the generator and mock each Promise-returning method in tests. You can then verify step order, retry behavior, and error propagation deterministically. For runner tests, include cases where yielded values are plain values, resolved Promises, and rejected Promises. Also test generator-thrown exceptions to confirm throw propagation works correctly. These tests provide confidence when refactoring legacy control flow into async and await while preserving behavior.
Common Pitfalls
A common pitfall is yielding non-Promise values unintentionally. Use Promise.resolve in the runner so plain values are handled consistently.
Another issue is losing stack context across asynchronous boundaries, especially in custom runners with incomplete error propagation.
Developers also forget cancellation concerns. Generator-style flow does not automatically cancel in-flight operations when callers stop waiting.
Finally, mixing generator-style async with callback-based APIs without wrappers leads to confusing control flow. Normalize async boundaries to Promises first.
Summary
- Generator-style async uses
yieldplus a Promise runner to model asynchronous steps. - The runner controls sequencing, error propagation, and optional retry logic.
- Async and await is usually simpler, but generators remain useful in specialized scenarios.
- Normalize yielded values through
Promise.resolvefor consistency. - Plan cancellation and error handling explicitly in custom runners.
Related reading
- Javascript How to control flow with async recursive tree traversal?
- JavaScript How to download JS asynchronously?
- JavaScript, Node.js is Array.forEach asynchronous?
- JavaScript skip await on pending fetch
- Javascript Git client
- JavaScript Math.random Normal distribution Gaussian bell curve?
- Javascript sync and async processes priority
- JavaScript wait for asynchronous function in if statement
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.