aboutsummaryrefslogtreecommitdiff
path: root/files/es/web/javascript/reference/global_objects/generator/next/index.html
blob: 94713c3b28013e20fdbb0741c81e4eb59500d2b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
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
---
<div>{{JSRef}}</div>

<p>El método <code><strong>next</strong></code><strong><code>()</code></strong> regresa un objeto con las propiedades <code>done</code> y <code>value</code>. También puedes pasar un parámetro al método <code>next</code> para enviar un valor al generador.</p>

<h2 id="Sintaxis">Sintaxis</h2>

<pre class="syntaxbox"><code><var>gen</var>.next(valor)</code></pre>

<h3 id="Parámetros">Parámetros</h3>

<dl>
 <dt><code>valor</code></dt>
 <dd>El valor a enviar al generador.</dd>
</dl>

<h3 id="Valor_de_retorno">Valor de retorno</h3>

<p>Un {{jsxref("Object")}} con dos propiedades:</p>

<ul>
 <li><code>done</code> (booleano)

  <ul>
   <li>Es <code>verdadero</code> si el iterador ya llegó al final de la secuencia. En este caso <code>valor</code> define opcionalmente el <em>valor de retorno</em> del iterador.</li>
   <li>Es <code>falso</code> si el iterador puede dar un siguiente valor en la secuencia. Es equivalente a no definir la propiedad <code>done</code>.</li>
  </ul>
 </li>
 <li><code>value</code> - cualquier valor Javascript regresado por el iterador. Éste puede ser omitido si <code>done</code> is <code>verdadero</code>.</li>
</ul>

<h2 id="Examples">Examples</h2>

<h3 id="Using_next()">Using <code>next()</code></h3>

<p>The following example shows a simple generator and the object that the <code>next</code> method returns:</p>

<pre class="brush: js">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 }"
</pre>

<h3 id="Sending_values_to_the_generator">Sending values to the generator</h3>

<p>In this example, <code>next</code> is called with a value. Note that the first call did not log anything, because the generator was not yielding anything initially.</p>

<pre class="brush: js">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 }"
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-generator.prototype.next', 'Generator.prototype.next')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-generator.prototype.next', 'Generator.prototype.next')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>


<p>{{Compat("javascript.builtins.Generator.next")}}</p>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">function*</a></code></li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators">Iterators and generators</a></li>
</ul>