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 used to pause the execution of an asynchronous function until a Promise is settled (either fulfilled or rejected), and to synchronously extract its fulfillment value. It fundamentally acts as syntactic sugar over the Promise.prototype.then() method, allowing asynchronous, promise-based behavior to be written in a synchronous, top-down execution style.
Syntax
expression: APromise, a thenable object, or any synchronous value to wait for.returnValue: The fulfilled value of the promise, or the value itself if the expression is not a promise.
Execution Mechanics
When the JavaScript engine encounters anawait operator, it performs the following sequence of operations:
- Expression Evaluation: The engine evaluates the
expressionimmediately on the right side of theawaitkeyword. - Context Suspension: The engine suspends the execution context of the current
asyncfunction. It yields control back to the caller of theasyncfunction, allowing the caller’s synchronous code to continue executing. Control only returns to the event loop once the synchronous call stack is completely empty. - Microtask Queuing: The engine waits for the evaluated Promise to settle. Once the Promise settles, the engine enqueues a microtask to resume the remainder of the
asyncfunction (everything following theawaitoperator). - Resumption:
- On Fulfillment: If the Promise fulfills, the
awaitexpression evaluates to the fulfillment value, and the function’s execution resumes. - On Rejection: If the Promise rejects, the
awaitexpression throws the rejection reason as an exception. This exception propagates synchronously and can be intercepted using a standardtry...catchblock.
- On Fulfillment: If the Promise fulfills, the
Evaluation Rules
The behavior ofawait changes depending on the type of the evaluated expression:
Scope Restrictions
Historically, theawait operator could only be used within the body of a function declared with the async keyword. Attempting to use await in a standard synchronous function results in a SyntaxError.
With the introduction of ECMAScript 2022, Top-Level Await is supported. This allows the await operator to be used outside of async functions, provided it is at the top level of an ECMAScript Module (.mjs or <script type="module">).
Because ES modules are evaluated bottom-up (post-order traversal), child modules (dependencies) are evaluated before the module that imports them. When used at the top level, await halts the evaluation of the current module and prevents parent modules (importers) from evaluating until the Promise settles.
Master JavaScript with Deep Grasping Methodology!Learn More





