This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.
The next() method of AsyncGenerator instances returns the next value in the sequence.
next() next(value)
value OptionalAn optional value used to modify the internal state of the generator. A value passed to the next() method will be received by yield
A Promise which when resolved returns an Object with two properties:
The following example shows a generator and the object that the next method returns:
// An async task. Pretend it's doing something more useful
// in practice.
function delayedValue(time, value) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(value), time);
});
}
async function* createAsyncGenerator() {
yield delayedValue(500, 1);
yield delayedValue(500, 2);
yield delayedValue(500, 3);
}
const asyncGen = createAsyncGenerator();
asyncGen.next().then((res) => console.log(res)); // { value: 1, done: false }
asyncGen.next().then((res) => console.log(res)); // { value: 2, done: false }
asyncGen.next().then((res) => console.log(res)); // { value: 3, done: false }
asyncGen.next().then((res) => console.log(res)); // { value: undefined, done: true }
In this example, next is called with a value.
Note: The first call does not log anything, because the generator was not yielding anything initially.
// An async task. Pretend it's doing something more useful
// in practice.
function sleep(time) {
return new Promise((resolve, reject) => {
setTimeout(resolve, time);
});
}
async function* createAsyncGenerator() {
while (true) {
await sleep(500);
const value = yield;
console.log(value);
}
}
async function main() {
const asyncGen = createAsyncGenerator();
// No log at this step: the first value sent through `next` is lost
console.log(await asyncGen.next(1)); // { value: undefined, done: false }
// Logs 2: the value sent through `next`
console.log(await asyncGen.next(2)); // { value: undefined, done: false }
}
main();
| Desktop | Mobile | Server | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
next |
63 | 79 | 55 | 50 | 12 | 63 | 55 | 46 | 12 | 8.0 | 63 | 12 | 1.0.0 | 1.0 | 10.0.0 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator/next