-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
73 lines (60 loc) · 1.34 KB
/
generator.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function* generatorFunc() {
console.log("Generator");
yield 'hello'
console.log("Generator 1");
yield 'world'
return '!'
}
const hw = generatorFunc()
// console.log(hw[Symbol.iterator]() === hw)
/**
*
*
*
任意一个对象的Symbol.iterator方法,等于该对象的遍历器生成函数,调用该函数会返回该对象的一个遍历器对象
*/
// console.log(hw.next());
// console.log(hw.next());
// console.log(hw.next());
function* gen() {
let result = yield '2 + 2 = ?'
console.log(result)
let a = yield result.replace('?', '4')
console.log(a)
}
const g = gen()
console.log(g.next())
console.log(g.next('222?'))
/**
* 伪随机数生成器
* @param seed
* @returns {Generator<number, void, *>}
*/
function* pseudoRandom(seed) {
let previous = seed
while (true) {
previous = previous * 16807 % 2147483647
// previous = next
yield previous
}
}
const gtr = pseudoRandom(1)
console.log(gtr.next().value)
console.log(gtr.next().value)
console.log(gtr.next().value)
let range = {
from: 1,
to: 5,
async *[Symbol.asyncIterator]() {
for (let value = this.from; value <= this.to; value++) {
await new Promise(resolve => setTimeout(resolve, 1000))
yield value
}
}
}
async function getValue() {
for await (let value of range) {
console.log('value', value)
}
}
getValue();