-
Notifications
You must be signed in to change notification settings - Fork 1
/
12-generators.js
28 lines (23 loc) · 995 Bytes
/
12-generators.js
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
function* listOfValues(){
for(let each of [3,4,5])
yield each
}
for(let value of listOfValues()){
console.log(`next value from generator: ${value}`);
}
for(let value of listOfValues()){
console.log(`next value from generator after second execution: ${value}`);
}
function* generatorWithDelegate(){
yield 1;
yield 2;
yield *listOfValues(); // yield* listOfValues()
}
let iterator = generatorWithDelegate();
console.log(`extended iterator next value: ${JSON.stringify(iterator.next())}`);
console.log(`extended iterator next value: ${JSON.stringify(iterator.next())}`);
console.log(`extended iterator next value: ${JSON.stringify(iterator.next())}`);
console.log(`extended iterator next value: ${JSON.stringify(iterator.next())}`);
console.log(`extended iterator next value: ${JSON.stringify(iterator.next())}`);
console.log(`extended iterator next value: ${JSON.stringify(iterator.next())}`);
console.log(`extended iterator next value: ${JSON.stringify(iterator.next())}`);