-
Notifications
You must be signed in to change notification settings - Fork 8k
/
index.md
116 lines (82 loc) · 2.87 KB
/
index.md
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
111
112
113
114
115
116
---
title: yield*
slug: Web/JavaScript/Reference/Operators/yield*
---
{{jsSidebar("Operators")}}
`La expresión` **`yield*`** es usada para delegar a otro {{jsxref("Statements/function*", "generator")}} u objeto iterable.
## Sintaxis
```
yield* [[expression]];
```
- `expression`
- : La expresión que retorna un objeto iterable
## Descripción
La expresión `yield*` itera sobre el operador realizando yield de cada valor retornado por este.
El valor de la expresion `yield*` es el valor retornado por el iterador en si mismo cuando es finalizado (ej., cuando `done` es true).
## Ejemplos
### Delegando a otro generator
En el siguiente código, los valores declarados con yield en `g1()` son devueltos por las llamadas a `next()` al igual que en `g2()`.
```js
function* g1() {
yield 2;
yield 3;
yield 4;
}
function* g2() {
yield 1;
yield* g1();
yield 5;
}
var iterator = g2();
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: 4, done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
```
### Otros objetos iterables
Además de los objetos generator, `yield*` también se puede usar `yield sobre otros tipos de iterables`, ej. arrays, strings u objetos arguments.
```js
function* g3() {
yield* [1, 2];
yield* "34";
yield* Array.from(arguments);
}
var iterator = g3(5, 6);
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: "3", done: false }
console.log(iterator.next()); // { value: "4", done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: 6, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
```
### El valor de la expresión `yield*`
`yield*` es una expresión, no una declaración, por lo que se evalua como un valor.
```js
function* g4() {
yield* [1, 2, 3];
return "foo";
}
var result;
function* g5() {
result = yield* g4();
}
var iterator = g5();
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: undefined, done: true },
// g4() returned { value: "foo", done: true } at this point
console.log(result); // "foo"
```
## Especificaciones
{{Specifications}}
## Compatibilidad con navegadores
{{Compat}}
## Ver también
- [Protocolos de iteración](/es/docs/Web/JavaScript/Referencia/Iteration_protocols)
- {{jsxref("Statements/function*", "function*")}}
- {{jsxref("Operators/function*", "function* expression")}}
- {{jsxref("Operators/yield", "yield")}}