| Method | Description | Common Use |
|---|---|---|
next(value) |
Resumes the generator’s execution from where it paused at the last yield. Optionally passes a value into the generator, which becomes the result of the yield expression. |
To continue normal execution. |
throw(error) |
Throws an error inside the generator at the point it was paused. If not caught with a try/catch, it will propagate outside the generator. |
To handle Promise rejections or inject errors. |
| **`return( |
| Scenario | Use Generators | Use Async/Await |
|---|---|---|
| Processing large or infinite data streams | ✅ Ideal - allows lazy evaluation and efficient iteration. | ❌ Not suitable - executes eagerly and expects finite results. |
| Controlling execution flow manually | ✅ Best choice - gives precise pause/resume control via next() and yield. |
❌ Engine controls execution automatically, limited manual control. |
| Simplifying asynchronous workflows | run(), but verbose. |
✅ D |
| Feature / Aspect | Generators | Async/Await |
|---|---|---|
| Control | Fine-grained, manual control via next() |
Automatic, sequential execution of awaited Promises |
| Use Case | Iteration, lazy evaluation, custom flow control | Simplifying asynchronous code readability and sequencing |
| Syntax Complexity | Requires helper or loop for async flows | Native, straightforward, built-in error handling |
| State Management | Maintains execution context, can pause/resume multiple times | Maintains context until function completes; no manual pause/resume |
| Memory / Laziness | Can produce values lazily, on demand |
| Capability | Responsibility | External Dependency | Hexagon |
|---|---|---|---|
| Shipment | Create, update, and store shipment details | Database | Shipment Hexagon |
| Tracking | Fetch and update location or delivery status | External tracking API | Tracking Hexagon |