Await Expressions and Loops

Learn how the for-await-of loop is used to iterate over every promise.

Using the for-await-of loop

Another special syntax enabled inside async functions is the for-await-of loop, which allows us to retrieve values from an iterable or an async iterable. An iterable is an object with a Symbol.iterator method that returns an iterator. An async iterable is an object with a Symbol.asyncIterator method that returns an iterator whose values are always promises. The for-await-of loop calls Promise.resolve() on each value returned from an iterable and then waits for each promise to resolve before continuing to the next iteration of the loop.

The most often used iterables in JavaScript are arrays, and we can use an array of promises with a for-await-of loop to process promises in sequence, like in this example:

Get hands-on with 1400+ tech skills courses.