JavaScript async function*

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

An async function* creates an async generator. You can yield values (including promises) and consume them with for await...of.

JavaScript Syntax

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

How It Works

  1. async function*. Returns an async generator.
  2. yield. Produces each value.
  3. for await...of. Consumes asynchronously.

Examples

Example 1 — Await then yield

Wait before producing.

JavaScript
async function* g() {
  const v = await Promise.resolve(9);
  yield v;
}
(async () => { for await (const x of g()) console.log(x); })();
Try it Editor

How It Works

  1. Await inside the generator.
  2. Then yield the result.

Example 2 — next() returns a promise

Manual async stepping.

JavaScript
async function* g() { yield "ok"; }
(async () => {
  const r = await g().next();
  console.log(r.value);
})();
Try it Editor

How It Works

  1. next() is async on async generators.

Example 3 — See for await...of

Preferred consumption style.

JavaScript
console.log("prefer for await...of for async generators");
Try it Editor

How It Works

  1. The statement page covers the loop form.

Frequently Asked Questions

When each step may wait on I/O or promises, such as paging an API.
Use for await...of.

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