The for await...of statement creates a loop iterating over async iterable objects as well as on sync iterables, including: built-in String, Array, Array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined async/sync iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Syntax

for await (variable of iterable) {
statement
}

Alternatively, the const, let, and var keywords can also be used to declare the iteration variable. For example:

for await (const variable of iterable) {
statement
}

variable

On each iteration a value of a different property is assigned to variable.

iterable

Object whose iterable properties are to be iterated over.

Iterating over async iterables

You can also iterate over an object that explicitly implements async iterable protocol:

For a more concrete example of iterating over an async generator using for await... of, consider iterating over data from an API. This example first creates an async iterator for a stream of data, then uses it to find the size of the response from the API.