Asynchronous generators

We have already learned about the interface that is implemented by all iterators:

interface Iterator<T> { 
  next(value?: any): IteratorResult<T>; 
  return?(value?: any): IteratorResult<T>; 
  throw?(e?: any): IteratorResult<T>; 
} 

However, we haven't yet learned about the interface that is implemented by all asynchronous iterators:

interface AsyncIterator<T> { 
  next(value?: any): Promise<IteratorResult<T>>; 
  return?(value?: any): Promise<IteratorResult<T>>; 
  throw?(e?: any): Promise<IteratorResult<T>>; 
} 

An asynchronous iterator returns a promise every time we invoke the next method. The following code snippet demonstrates how asynchronous iterators can be very useful when used in conjunction with asynchronous functions:

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.