--- title: Generator.prototype.next() slug: Web/JavaScript/Reference/Global_Objects/Generator/next translation_of: Web/JavaScript/Reference/Global_Objects/Generator/next original_slug: Web/JavaScript/Referencia/Objetos_globales/Generador/next ---
El método next() regresa un objeto con las propiedades done y value. También puedes pasar un parámetro al método next para enviar un valor al generador.
gen.next(valor)
valorUn {{jsxref("Object")}} con dos propiedades:
done (booleano)
verdadero si el iterador ya llegó al final de la secuencia. En este caso valor define opcionalmente el valor de retorno del iterador.falso si el iterador puede dar un siguiente valor en la secuencia. Es equivalente a no definir la propiedad done.value - cualquier valor Javascript regresado por el iterador. Éste puede ser omitido si done is verdadero.next()The following example shows a simple generator and the object that the next method returns:
function* gen() {
yield 1;
yield 2;
yield 3;
}
var g = gen(); // "Generator { }"
g.next(); // "Object { value: 1, done: false }"
g.next(); // "Object { value: 2, done: false }"
g.next(); // "Object { value: 3, done: false }"
g.next(); // "Object { value: undefined, done: true }"
In this example, next is called with a value. Note that the first call did not log anything, because the generator was not yielding anything initially.
function* gen() {
while(true) {
var value = yield null;
console.log(value);
}
}
var g = gen();
g.next(1);
// "{ value: null, done: false }"
g.next(2);
// 2
// "{ value: null, done: false }"
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('ES2015', '#sec-generator.prototype.next', 'Generator.prototype.next')}} | {{Spec2('ES2015')}} | Initial definition. |
| {{SpecName('ESDraft', '#sec-generator.prototype.next', 'Generator.prototype.next')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Generator.next")}}