> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Await

The `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

```typescript theme={"dark"}
await expression;
```

* **`expression`**: A `Promise`, 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>` or `PromiseLike<T>`, the resulting static type of the `await` expression is `Awaited<T>`. Because `await` and `Awaited` unwrap promises recursively, if `T` is itself a Promise, the expression resolves to the deeply unwrapped type. For example, applying `await` to an operand of type `Promise<Promise<string>>` resolves to `string`, not `Promise<string>`.
* If the operand is a non-promise type `U`, the TypeScript type checker simply resolves the static type to `U`. (Note: At runtime, the JavaScript engine implicitly wraps and unwraps this value via `Promise.resolve()`, but the TypeScript static type remains `U`).

```typescript theme={"dark"}
export {}; // Ensures the file is treated as a module to allow top-level await

declare const nestedPromise: Promise<Promise<string>>;
declare const syncValue: number;

// TypeScript infers 'result1' as 'string' via Awaited<Promise<Promise<string>>>
const result1 = await nestedPromise; 

// TypeScript infers 'result2' as 'number' via Awaited<number>
const result2 = await syncValue; 
```

## Execution Context

The `await` operator is syntactically valid only in specific execution contexts:

1. Inside a function, method, or arrow function marked with the `async` modifier.
2. Inside an async generator function (`async function*`).
3. At the top level of a module (Top-Level Await), provided the TypeScript compiler `target` is `es2017` or higher and the `module` option is set to a compatible format (e.g., `es2022`, `esnext`, `node16`, `nodenext`).

```typescript theme={"dark"}
// Valid: Inside an async function
async function process(): Promise<void> {
    const data = await Promise.resolve(42);
}

// Valid: Top-level await (in a module context)
export const config = await Promise.resolve({ env: "prod" });
```

## Control Flow and Rejection

When the JavaScript engine encounters `await`, 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 `await` expression evaluates to the fulfillment value, and execution resumes.
* **Rejection**: If the promise rejects, the `await` operator synchronously throws the rejection reason as an exception within the async function's execution context. This requires standard `try...catch` blocks for error interception.

```typescript theme={"dark"}
async function handleRejection(): Promise<void> {
    try {
        // If the promise rejects, execution immediately jumps to the catch block
        const value = await Promise.reject(new Error("Failure"));
    } catch (error: unknown) {
        // 'error' contains the rejection reason
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
