-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_example.js
More file actions
217 lines (195 loc) · 8.71 KB
/
test_example.js
File metadata and controls
217 lines (195 loc) · 8.71 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//1.parallel then chain
var a=new Promise(function(res,rej){
setTimeout(function(){res("haha1");},1000);
});
a.then(function(val){console.log(val);return "haha2-succ";},
function(reason){console.log(reason);return "haha2-fail";}
).then(function(val){
console.log(val);
return new Promise(function(res,rej){setTimeout(function(){rej("haha3-fail");},1000);})
}).catch(function(reason){
console.log(reason);
return new Promise(function(res,rej){setTimeout(function(){res("haha4-succ");},1000);})
}).then(function(val){console.log(val);return 1;})
a.then(function(val){console.log(val);return "b2-succ";},
function(reason){console.log(reason);return "b2-fail";}
).then(function(val){
console.log(val);
return new Promise(function(res,rej){setTimeout(function(){rej("b3-fail");},1000);})
}).catch(function(reason){
console.log(reason);
return new Promise(function(res,rej){setTimeout(function(){res("b4-succ");},1000);})
}).then(function(val){console.log(val);return 1;})
//2.return a then chain
var a=new Promise(function(res,rej){
setTimeout(function(){res("hah1");},1000);
}).then(function(val){
console.log(val);
return new Promise(function(res,rej){
setTimeout(function(){res("hah2");},1000);
}).then(function(val){
console.log(val);
return "hah3";
});
}).then(function(val){console.log(val);});
//3.another return a then chain
var a=new Promise(function(res,rej){
setTimeout(function(){res("hah1");},1000);
}).then(function(val){
console.log(val);
return new Promise(function(res,rej){
setTimeout(function(){res("hah2");},1000);
}).then(function(val){
console.log(val);
return new Promise(function(res,rej){setTimeout(function(){res("hah3");},1000);});
});
}).then(function(val){console.log(val);});
//4.again another return a then chain
var wen=Promise.resolve("ddd").then(function(value){
console.log(value);
return new Promise(function(res,rej){
setTimeout(function(){res("hah1");},1000);
}).then(function(val){
console.log(val);
return Promise.resolve("1").then(function(val){
return Promise.resolve("2");
});
});
});
wen.then(function(value){console.log(value);});
//5.microtask + macrotask queue
setTimeout(function(){
console.log("s1");
new Promise(function(res,rej){
setTimeout(function(){res("promise0");},0);
}).then(function(value){
console.log(value);return "promise1";
}).then(function(value){
console.log(value);return "promise2";
}).then(function(value){
console.log(value);return "promise3";
}).then(function(value){
console.log(value);return "promise4";
}).then(function(value){
console.log(value);return "promise5";
}).then(function(value){console.log(value);return "promise6";});
},1000);
setTimeout(function(){console.log("s2");},1000);
setTimeout(function(){console.log("s3");},1000);
setTimeout(function(){console.log("s4");},1000);
setTimeout(function(){console.log("s5");},1000);
console.log("start");
//6.another macritask+microtask
setTimeout(function(){
console.log("s1");
new Promise(function(res,rej){
res("promise0");
}).then(function(value){
console.log(value);return "promise1";
}).then(function(value){
console.log(value);return "promise2";
}).then(function(value){
console.log(value);return "promise3";
}).then(function(value){
console.log(value);return "promise4";
}).then(function(value){
console.log(value);return "promise5";
}).then(function(value){console.log(value);return "promise6";});
},1000);
setTimeout(function(){console.log("s2");},1000);
setTimeout(function(){console.log("s3");},1000);
setTimeout(function(){console.log("s4");},1000);
setTimeout(function(){console.log("s5");},1000);
console.log("start");
//7.a simple microtask+macrotask log:1,3,dd,2;
console.log(1);
setTimeout(function(){console.log(2);},0);
Promise.resolve("dd").then(function(val){console.log(val);});
console.log(3);
//8.horrible nesting to check microtask,log:dd,ddd,dddd,...,ddddddddd
Promise.resolve("dd").then(function(val){
console.log(val);
return Promise.resolve("ddd").then(function(val){
console.log(val);return "dddd";
}).then(function(val){
console.log(val);return "ddddd";
}).then(function(val){
console.log(val);return "dddddd";
}).then(function(val){
console.log(val);return new Promise(function(res,rej){setTimeout(function(){res("ddddddd");},1000);
}).then(function(val){
console.log(val);return "dddddddd";
});
}).then(function(val){
console.log(val);return "ddddddddd";});
}).then(function(val){
console.log(val);
});
//9.thenable object:
//a.//log:function,haha
Promise.resolve({then:function(d){console.log(d);d("haha")}}).then(function(val){console.log(val);});
//b.//log:1
Promise.resolve({then:function(){console.log(1);}}).then(function(val){console.log(val);});
//c.//log:jd,hshs;
new Promise(function(res,rej){res({then:function(resolve){console.log("jd");resolve("hshs")}})}).then(function(val){console.log(val);});
//d.//log:jd
var wen=new Promise(function(res,rej){setTimeout(function(){res({then:function(){console.log("jd");}})},1000);});
wen.then(function(val){console.log(val);});
//e.//log:function,jd
var wen=new Promise(function(res,rej){setTimeout(function(){res({then:function(d){console.log(d);d("jd");}})},1000);});
wen.then(function(val){console.log(val);});
//f.//log:undefined
Promise.resolve({then:function(d){d()}}).then(function(val){console.log(val);});
//10.res(promise);both log:dd
//a.
var wen=new Promise(function(res,rej){res(new Promise(function(res,rej){setTimeout(function(){res("dd")},1000)}));});
wen.then(function(val){console.log(val);});
//b.
var wen=new Promise(function(res,rej){res(new Promise(function(res,rej){res(Promise.resolve("dd"));}));});
wen.then(function(val){console.log(val);});
//11.res(thenable/promise) plus nesting
//a.log:dd
Promise.resolve({then:function(d){d({then:function(f){f("dd");}})}}).then(function(val){console.log(val);});
//b.log:dd
Promise.resolve({then:function(d){d({then:function(f){f(Promise.resolve({then:function(ll){ll("dd");}}));}})}}).then(function(val){console.log(val);});
//c.log:dd
Promise.resolve({then:function(d){d({then:function(f){f(Promise.resolve({then:function(ll){ll(Promise.resolve("dd"));}}));}})}}).then(function(val){console.log(val);});
//d.log:dd
Promise.resolve({then:function(d){d({then:function(f){f(new Promise(function(res,rej){setTimeout(function(){res("dd")},1000)}));}})}}).then(function(val){console.log(val);});
//e.log:dd
Promise.resolve({then:function(d){d({then:function(f){f(Promise.resolve({then:function(ll){ll(Promise.resolve(new Promise(function(res,rej){setTimeout(function(){res({then:function(mm){mm("dd");}});},1000);})));}}));}})}}).then(function(val){console.log(val);});
//12. a VS b VS c
//a.log:jd --(no d(),so just log "jd",not log undefined.)
var wen=new Promise(function(res,rej){setTimeout(function(){res({then:function(d){console.log("jd");}})},1000);});
wen.then(function(val){console.log(val);});
//b.log:dd,ddd
Promise.resolve("dd").then(function(val){console.log(val);return "ddd";}).then(function(val){console.log(val);});
//c.log:dd,undefined
Promise.resolve("dd").then(function(val){console.log(val);}).then(function(val){console.log(val);});
//13.reject,just use whatever passed in;log:object
Promise.reject({then:function(d){console.log(1);d(2);}}).catch(function(val){console.log(val);});
//14.res(false/undefined/null)
//a.log:false
new Promise(function(res,rej){res(false);}).then(function(val){console.log(val);});
//b.log:1,false
Promise.resolve({then:function(d){console.log(1);d(false);}}).then(function(val){console.log(val);});
//c.log:null
new Promise(function(res,rej){res(null);}).then(function(val){console.log(val);});
//d.log:undefined
new Promise(function(res,rej){res(undefined);}).then(function(val){console.log(val);});
//15.new Promise内部为同步过程 验证样例
//打印1,2,然后才打印3
Promise.resolve(new Promise(function(res,rej){console.log(1);res(2);})).then(function(val){console.log(val);});
Promise.resolve(3).then(function(val){console.log(val);});
//16.打印顺序是d,1,null,undefiined,dd,
//而不是d,1,dd,null,undefined
//充分证明,下面的样例b,先microThen,发现superResult是thenable,于是修改father的状态,重新跑一遍
//即再跑的过程中,又实用micro
//a.
new Promise(function(res,rej){res('d');}).then(function(val){console.log(val);});
//b.
Promise.resolve({then:function(d){console.log(1);d('dd');}}).then(function(val){console.log(val);});
//c.log:null
new Promise(function(res,rej){res(null);}).then(function(val){console.log(val);});
//d.log:undefined
new Promise(function(res,rej){res(undefined);}).then(function(val){console.log(val);});