-
Notifications
You must be signed in to change notification settings - Fork 0
/
es6_promise.js
148 lines (127 loc) · 2.81 KB
/
es6_promise.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function sleep(duration) {
return new Promise(function(resolve, reject) {
console.log("c");
setTimeout(resolve,duration);
})
}
async function foo(name){
await sleep(2000)
console.log(name)
}
async function foo2(){
await foo("a");
await foo("b");
}
foo2();
function sleep(duration) {
return new Promise(function(resolve, reject) {
console.log("aaaa");
setTimeout(resolve,duration);
})
}
sleep(1000).then( ()=> console.log("finished"));
var r = new Promise(function(resolve, reject){
console.log("a");
resolve()
});
setTimeout(()=>console.log("d"), 0)
r.then(() => console.log("c"));
console.log("b")
setTimeout(()=>console.log("d"), 0)
var r = new Promise(function(resolve, reject){
resolve()
});
r.then(() => {
var begin = Date.now();
while(Date.now() - begin < 1000);
console.log("c1")
new Promise(function(resolve, reject){
resolve()
}).then(() => console.log("c2"))
});
function sleep(duration) {
return new Promise(function(resolve, reject) {
console.log("b");
setTimeout(resolve,duration);
})
}
console.log("a");
sleep(5000).then(()=>console.log("c"));
function sleep(duration) {
return new Promise(function(resolve, reject) {
console.log("c");
setTimeout(resolve,duration);
})
}
async function foo(){
console.log("a")
await sleep(2000)
console.log("b")
}
foo();
function sleep(duration) {
return new Promise(function(resolve, reject) {
setTimeout(resolve,duration);
})
}
async function foo(name){
await sleep(2000)
console.log(name)
}
async function foo2(){
await foo("a");
await foo("b");
}
foo2()
function sleep(duration, color){
return new Promise(function(resolve){
console.log(color);
setTimeout(resolve, duration);
})
}
// async function changeColor(duration,color){
// // document.getElementById("traffic-light").style.background = color;
// console.log(color);
// await sleep(duration);
// }
async function main(){
while(true){
await sleep(3000,"green");
await sleep(1000, "yellow");
await sleep(2000, "red");
}
}
main()
var o = {
myfunc: function(){}
}
console.log(o.myfunc.name)
// sync1
// sync2
// promise
// setTimeoutPromise
// pro_then
// pro_timeout
// setTimeout1
// last_setTimeout
console.log('sync1');
setTimeout(function () {
console.log('setTimeout1')
}, 0);
var promise = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('setTimeoutPromise')
}, 0);
console.log('promise');
resolve();
});
promise.then(() => {
console.log('pro_then');
setTimeout(() => {
console.log('pro_timeout');
}, 0)
})
setTimeout(function () {
console.log('last_setTimeout')
}, 0);
console.log('sync2');