Il metodo next
()
ritorna un oggetto con due proprietà done
and value
. Puoi anche fornire un parametro al metodo next per trasmettere un valore al generatore.
Syntassi
gen.next(value)
Parametri
value
- Il valore trasmesso al generatore
Return value
Un Oggetto con due proprietà:
done
(boolean)- Ha il valore
true
se l' iteratore è oltre la fine della sequenza iterata. In questo casovalue
opzionalmente specifica il valore di ritorno dell' iteratore. - Ha il valore
false
se l'iteratore è stato capace di generare il valore successivo nella sequenza. Questo equivale nello non specificare la proprietà done interamente.
- Ha il valore
value
- ogni valore Javascript ritornato dall'iteratore. Può essere omesso quando done è true
Examples
Using next()
Il seguente esempio mostra semplice generatore e un oggetto che il metodo next ritorna:
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 }"
Mandare valori al generatore
In questo esempio, next è stato chiamato con un valore. Nota che la prima chiamata non ha registrato nulla, perche il generatore non ha raccolto nulla inizialmente.
function* gen() {
while(true) {
var value = yield null;
console.log(value);
}
}
var g = gen();
g.next(1);
// "{ value: null, done: false }"
g.next(2);
// "{ value: null, done: false }"
// 2
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Generator.prototype.next' in that specification. |
Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) The definition of 'Generator.prototype.next' in that specification. |
Draft |
Browser compatibility
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | 13 | 26 (26) | No support | (Yes) | 10 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 5.1 | (Yes) | 26.0 (26) | ? | ? | 10 |