Dev Helpers
← Back

Yield values from an async generator

Yield values from an async generator using TypeScript.

Open this example in the interactive library

Source code

const load = async (id: number): Promise<{ id: number; ready: boolean }> => ({
  id,
  ready: true,
});
console.log(
  await (async () => {
    async function* rows(): AsyncGenerator<number> {
      yield 1;
      yield 2;
    }
    const out: number[] = [];
    for await (const n of rows()) out.push(n);
    return out;
  })(),
);

Sample output

[ 1, 2 ]