-
Notifications
You must be signed in to change notification settings - Fork 1
/
Generator+promise.html
74 lines (69 loc) · 1.63 KB
/
Generator+promise.html
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
74
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
// function red(){
// console.log('red');
// }
// function green(){
// console.log('green');
// }
// function yellow(){
// console.log('yellow');
// }
//
// var tic = function(timmer, cb){
// return new Promise(function(resolve, reject) {
// setTimeout(function() {
// cb();
// resolve();
// }, timmer);
// });
// };
//
// var d = new Promise(function(resolve, reject){resolve();});
// var step = function(def) {
// def.then(function(){
// return tic(3000, red);
// }).then(function(){
// return tic(2000, green);
// }).then(function(){
// return tic(1000, yellow);
// }).then(function(){
// step(def);
// });
// }
//
// step(d);
var tic = function(timmer, str){
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log(str);
resolve(1);
}, timmer);
});
};
function *gen(){
yield tic(3000, 'red');
yield tic(1000, 'green');
yield tic(2000, 'yellow');
}
var iterator = gen();
var step = function(gen, iterator){
var s = iterator.next();
if (s.done) {
step(gen, gen());
} else {
s.value.then(function() {
step(gen, iterator);
});
}
}
step(gen, iterator);
</script>
</body>
</html>