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

Async Iteration vs Sync Iteration

Understand the key differences between Symbol.iterator and Symbol.asyncIterator, and when async iteration is necessary.

Async Iteration vs Sync Iteration

What you'll learn: The fundamental differences between synchronous and asynchronous iteration, and when each is appropriate.

The Core Difference

While Symbol.iterator produces values immediately (synchronously), Symbol.asyncIterator produces values that may arrive later (asynchronously). Think of sync iteration like picking apples already in a basket—you grab each one instantly. Async iteration is like waiting for apples to arrive on a conveyor belt—you must wait for each one before processing it.

Key Distinctions

Synchronous iteration:

  • Uses Symbol.iterator
  • Returns plain iterator objects with {value, done}
  • Works with for...of loops
  • All values are immediately available
  • Methods like next() return results instantly

Asynchronous iteration:

  • Uses Symbol.asyncIterator
  • Returns promises that resolve to {value, done}
  • Works with for await...of loops
  • Values may arrive over time (network requests, file streams, timers)
  • Methods like next() return promises

When Async Iteration Is Necessary

You need async iteration when your data source produces values asynchronously:

  • Reading large files chunk-by-chunk
  • Processing streaming API responses
  • Handling real-time data feeds
  • Database query results arriving incrementally
  • Any scenario where waiting for the next value involves I/O or timers

If all your data is already in memory and available immediately, stick with synchronous iteration—it's simpler and more efficient. Use async iteration only when you genuinely need to wait for values to arrive over time.

Key Takeaway: Sync iteration (Symbol.iterator) is for immediately available data, while async iteration (Symbol.asyncIterator) is for data that arrives over time—choose based on whether your data source requires waiting.