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.
await operator is a unary operator that suspends the execution of an asynchronous function until a Promise is settled (either fulfilled or rejected). In TypeScript, it inherently acts as a static type-unwrapping mechanism, extracting the deeply resolved value type from a Promise or PromiseLike object.
Syntax
expression: APromise, a thenable object, or any synchronous value waiting to be resolved.
Type Resolution and Awaited<T>
TypeScript’s type checker evaluates the await operator by recursively unwrapping the Promise type. To model this exact resolution behavior, TypeScript 4.5 introduced the Awaited<T> utility type, which mirrors the recursive unwrapping performed by the await operator.
- If the operand is of type
Promise<T>orPromiseLike<T>, the resulting static type of theawaitexpression isAwaited<T>. BecauseawaitandAwaitedunwrap promises recursively, ifTis itself a Promise, the expression resolves to the deeply unwrapped type. For example, applyingawaitto an operand of typePromise<Promise<string>>resolves tostring, notPromise<string>. - If the operand is a non-promise type
U, the TypeScript type checker simply resolves the static type toU. (Note: At runtime, the JavaScript engine implicitly wraps and unwraps this value viaPromise.resolve(), but the TypeScript static type remainsU).
Execution Context
Theawait operator is syntactically valid only in specific execution contexts:
- Inside a function, method, or arrow function marked with the
asyncmodifier. - Inside an async generator function (
async function*). - At the top level of a module (Top-Level Await), provided the TypeScript compiler
targetises2017or higher and themoduleoption is set to a compatible format (e.g.,es2022,esnext,node16,nodenext).
Control Flow and Rejection
When the JavaScript engine encountersawait, it yields execution control back to the calling context and pushes the remainder of the async function onto the microtask queue.
- Fulfillment: If the promise fulfills, the
awaitexpression evaluates to the fulfillment value, and execution resumes. - Rejection: If the promise rejects, the
awaitoperator synchronously throws the rejection reason as an exception within the async function’s execution context. This requires standardtry...catchblocks for error interception.
Master TypeScript with Deep Grasping Methodology!Learn More





