--- 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 ---
{{JSRef}}

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.

Sintaxis

gen.next(valor)

Parámetros

valor
El valor a enviar al generador.

Valor de retorno

Un {{jsxref("Object")}} con dos propiedades:

Examples

Using 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 }"

Sending values to the generator

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 }"

Specifications

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')}}  

Browser compatibility

{{Compat("javascript.builtins.Generator.next")}}

See also