# Generator 异步编程
使用 setTimeout 模拟异步操作:
let getDataOne = (cb) => {
setTimeout(function () {
cb('dummy data one')
}, 1000)
}
let getDataTwo = (cb) => {
setTimeout(function () {
cb('dummy data two')
}, 1000)
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
调用上边的两个异步函数:
getDataOne(data => console.log(data))
getDataTwo(data => console.log(data))
// 如果想顺序执行就会需要回调嵌套了
1
2
3
2
3
使用 Generator 改造代码:
let generator
let getDataOne = () => {
setTimeout(function () {
generator.next('dummy data one')
}, 1000)
}
let getDataTwo = () => {
setTimeout(function () {
generator.next('dummy data two')
}, 1000)
}
function* main () {
let dataOne = yield getDataOne()
let dataTwo = yield getDataTwo()
console.log(dataOne)
console.log(dataTwo)
}
generator = main()
generator.next() // 两秒后一下都打印了
// {value: undefined, done: false}
// dummy data one
// dummy data two
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25