forked from yeyan1996/practical-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
59 lines (48 loc) · 1.46 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
//自执行generator函数
const data = "{a:1,b:2}"
const data2 = "{c:3,d:4}"
const data3 = "{e:5,f:6}"
const api = function (data) {
return new Promise(resolve => {
setTimeout(() => {
resolve(data)
}, 1000)
})
}
function* func() {
let res = yield api(data)
console.log(res)
let res2 = yield api(data2)
console.log(res2)
let res3 = yield api(data3)
console.log(res3)
console.log(res, res2, res3)
}
function makePromisify(source) {
if (source.then && typeof source.then === "function") return source
return Promise.resolve(source)
}
function run(generatorFunc) {
let it = generatorFunc()
let result = it.next()
return new Promise((resolve, reject) => {
const next = function (result) {
if (result.done) {
resolve(result.value)
}
//保证返回的是一个promise
result.value = makePromisify(result.value)
result.value.then(res => {
//将promise的返回值res传入iterator迭代器的next方法中,作为yield后面表达式的返回值
//it.next将停止的yield继续执行到下一个yield,返回的result是一个value,done属性组成的对象
let result = it.next(res)
//递归执行next函数
next(result)
}).catch(err => {
reject(err)
})
}
next(result)
})
}
run(func)