> ## 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.

# JavaScript For-await-of Loop

The `for-await-of` statement creates a loop iterating over asynchronous iterable objects as well as synchronous iterables. It implicitly awaits the resolution of each yielded Promise before executing the loop block, ensuring sequential execution of asynchronous operations within the iteration.

## Syntax

```javascript theme={"dark"}
for await (variable of iterable) {
  // statement
}
```

* **`variable`**: Receives the resolved value of the current iteration. Can be declared using `const`, `let`, or `var`, or it can be an assignment target (e.g., a previously declared variable or a destructured object/array).
* **`iterable`**: An object that implements the `[Symbol.asyncIterator]()` method or the synchronous `[Symbol.iterator]()` method.

## Execution Context

Because it utilizes the `await` keyword under the hood, a `for-await-of` loop can only be executed within an `async` function, an async generator function, or at the top level of a module (Top-Level Await).

## Iteration Mechanics

1. **Protocol Evaluation**: The loop first checks if the `iterable` possesses a `[Symbol.asyncIterator]` method. If absent, it falls back to the synchronous `[Symbol.iterator]` method.
2. **Promise Resolution**: For each iteration, the loop calls the iterator's `next()` method.
   * If the yielded value is a Promise, the loop pauses execution of the block until the Promise settles.
   * If the yielded value is not a Promise, it is implicitly wrapped in a resolved Promise (equivalent to `Promise.resolve(value)`).
3. **Value Assignment**: Once the Promise resolves, the resolved value is assigned to the `variable`, and the loop body executes.
4. **Rejection Handling**: If a yielded Promise rejects, the loop immediately terminates, and the rejection is thrown as an exception within the current scope. This can be intercepted using a standard `try...catch` block surrounding the loop.

## Control Flow

* **`break`**: Immediately terminates the loop. If the iterator has a `return()` method (used for cleanup in generators), the `for-await-of` loop will automatically invoke it before exiting.
* **`continue`**: Bypasses the remaining statements in the current loop body and proceeds to request and await the next value from the iterator.

## Technical Distinctions

* **Sequential vs. Concurrent**: `for-await-of` processes iterations sequentially. It will not request the next item from the iterator until the current item's Promise has resolved and the loop body has finished executing.
* **Standard `for...of` with Promises**: If a standard `for...of` loop is used on an iterable containing Promises, it iterates synchronously, assigning the pending Promise objects directly to the variable. `for-await-of` unwraps these Promises, assigning the underlying resolved values to the variable.

## Example of Mechanics

```javascript theme={"dark"}
async function processAsyncIterable(asyncIterable) {
  try {
    for await (const resolvedValue of asyncIterable) {
      console.log(resolvedValue); // Executes only after the current Promise resolves
    }
  } catch (error) {
    // Catches any rejection from the yielded Promises
    console.error("Iteration terminated due to rejection:", error);
  }
}
```

<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 JavaScript 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>
