TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
for-await-of statement creates a loop that iterates over asynchronous iterable objects—objects that implement the [Symbol.asyncIterator]() method. It sequentially pauses the execution of the containing async function or block at the loop header until each yielded Promise resolves, extracting the fulfilled value into the loop variable before executing the block.
Technical Mechanics
Under the hood, thefor-await-of loop interacts with the AsyncIterator protocol. When the loop initializes, it calls the [Symbol.asyncIterator]() method on the target object to retrieve an asynchronous iterator.
During each iteration:
- The loop invokes the
next()method on the iterator. next()returns aPromise<IteratorResult<T>>.- The loop implicitly
awaits this Promise at the loop header. - Upon resolution, if the
IteratorResulthasdone: false, thevalueproperty is assigned to the loop variable, and the block executes. - If the
IteratorResulthasdone: true, the loop terminates.
TypeScript Typing
TypeScript strictly types thefor-await-of loop using the built-in AsyncIterable<T> and AsyncIterator<T> interfaces. The loop variable’s type is automatically inferred based on the generic type parameter T of the iterable.
Synchronous Iterable Fallback
If the target object does not implement[Symbol.asyncIterator]() but does implement the synchronous [Symbol.iterator]() (such as a standard Array), for-await-of will consume the synchronous iterable. If the synchronous iterable yields Promises (e.g., Iterable<Promise<T>>), the loop will await each Promise sequentially.
Compiler Configuration
To usefor-await-of in TypeScript, specific tsconfig.json compiler options must be configured depending on your target environment:
lib: Must include"ES2018.AsyncIterable"(or a broader library like"ES2018") to provide the global type definitions forSymbol.asyncIterator,AsyncIterable, andAsyncIterator.target: If set to"ES2018"or higher, TypeScript emits nativefor-await-ofsyntax. If targeting older environments (such as"ES5"or"ES6"), TypeScript automatically downlevels the loop using the__asyncValueshelper function.- Downleveling: Unlike synchronous iteration (
for...of),for-await-ofdoes not require the"downlevelIteration": trueflag to be downleveled. TypeScript always transpiles it when targeting pre-ES2018 environments. However, a runtime polyfill forSymbol.asyncIteratormust be present in the execution environment for the emitted code to function correctly.
Master TypeScript with Deep Grasping Methodology!Learn More





