JavaScript for await...of

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

for await...of loops over async iterables. Each iteration awaits the next value, so it works with async generators and objects that implement Symbol.asyncIterator.

JavaScript Syntax

JavaScript
async function* stream() {
  yield "one";
  yield "two";
}
(async () => {
  for await (const item of stream()) {
    console.log(item);
  }
})();
Try it Editor

How It Works

  1. async iterable. stream() is an async generator.
  2. await each step. The loop waits before the next body run.
  3. Log. Prints one, then two.

Examples

Example 1 — Awaiting promises in an array

Arrays are sync iterables; map to promises separately.

JavaScript
const ps = [Promise.resolve(1), Promise.resolve(2)];
(async () => {
  for (const p of ps) console.log(await p);
})();
Try it Editor

How It Works

  1. for...of over promises.
  2. await each promise inside.

Example 2 — break works

Stop early.

JavaScript
async function* g() { yield 1; yield 2; yield 3; }
(async () => {
  for await (const n of g()) {
    console.log(n);
    if (n === 2) break;
  }
})();
Try it Editor

How It Works

  1. break exits the loop.
  2. Later yields are not consumed.

Example 3 — Vs for...of

Use for...of for sync iterables.

JavaScript
for (const n of [1, 2]) console.log(n);
Try it Editor

How It Works

  1. No await needed for plain arrays.

Frequently Asked Questions

Arrays are sync iterables. Prefer for...of, or await promises inside the loop body.
Inside an async function, or at the top level of a module.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful