-
Notifications
You must be signed in to change notification settings - Fork 1
/
generators.js
58 lines (48 loc) · 1.72 KB
/
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
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
/** Generators **/
// Generators are created by declaring a generator function,
// which returns a generator object (e.g. g) that can then be iterated using
// Array.from(g), [...g], or for value of g.
// We use the symbol * to mark a function as a generator
// and 'yield' to emit an element sequence.
function* generator () {
yield 'f'
yield 'o'
yield 'o'
}
[...generator()] // ['f', 'o', 'o']
// Generator object follow both iterable and iterator protocol.
var g = generator()
// It is an iterable because if has na @@iterator.
typeof g[Symbol.iterator] === 'function' // true
// It is an iterator because if has the .next method.
typeof g.next === 'function' // true
// The iterator for a generator is the generator itself.
g[Symbol.iterator]() === g // true
[...g] // ['f','o','o']
Array.from(g) // ['f', 'o', 'o']
for(let item of g) {
console.log(item)
// 'f'
// 'o'
// 'o'
}
// Whenever the 'yield' expression is reached, the value is emitted
// by the iterator and the function execution is suspended.
function* generator () {
yield 'foo'
console.log('and')
yield 'bar'
}
var foo = generator()
foo.next() // emits { value: 'f', done: false } and suspends
foo.next() // logs 'and', emits { value: 'bar', done: false }, and suspends
foo.next() // emits {value: undefined, done: true } and finishes
// Whenever .next() is called on a generator, there's four events that can suspend the execution:
// * yield - emits the next value in the sequence
// * return - returns the last value in the sequence
// * throw - stops the execution in the generator entirely
// Use 'yield*' to delegate to other generator function.
function* generator () {
yield* 'foo'
}
console.log([...generator()]) // ['f','o','o']