Course contentsShow
← JavaScript
Lesson 2037 of 2,72342. Iteration Protocols Deep DiveFree lesson

When to Prefer For-Await-Of

Recognize scenarios where for-await-of simplifies async iteration compared to manual promise chaining or recursion.

When to Prefer For-Await-Of

You'll learn when for-await-of is the clearest, simplest choice for handling async iteration.

The Right Tool for the Job

By now you've seen for-await-of in action. But when should you prefer it over alternatives like manual promise chaining, recursive functions, or Promise.all?

The golden rule: Choose for-await-of when you need sequential processing of async values where order matters or results depend on previous iterations.

Clear Scenarios for For-Await-Of

1. Reading Streams or Paginated APIs

When consuming data that arrives in chunks (like readable streams or API pages), for-await-of handles the complexity for you—no manual recursion needed.

2. Sequential Dependencies

When each async operation depends on the result of the previous one, for-await-of makes the flow obvious. Manual promise chaining becomes a nested mess.

3. Simple Error Handling

A single try-catch around for-await-of is cleaner than attaching .catch() to every promise in a chain.

4. Readability Over Raw Speed

If you don't need parallel execution (where Promise.all shines), for-await-of reads like synchronous code, making maintenance easier.

When NOT to Prefer It

  • Parallel execution: If tasks are independent and can run simultaneously, use Promise.all instead
  • Single async operation: Plain await is simpler for one-off promises
  • Performance-critical loops: The sequential nature may be slower than parallel approaches

Key Takeaway: Prefer for-await-of when you need readable, sequential async iteration with straightforward error handling—especially for streams, pagination, or dependent operations. Use Promise.all when parallelism matters more than order.