回答編集履歴
1
chousei
test
CHANGED
@@ -139,3 +139,99 @@
|
|
139
139
|
he
|
140
140
|
|
141
141
|
※すでにasnycFuncが処理済みなのでhe likesは表示されない
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
# さらに
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
```javascript
|
150
|
+
|
151
|
+
const asyncFunc=()=>{
|
152
|
+
|
153
|
+
return new Promise(resolve=>{
|
154
|
+
|
155
|
+
setTimeout(()=>{
|
156
|
+
|
157
|
+
console.log(3);
|
158
|
+
|
159
|
+
resolve("he");
|
160
|
+
|
161
|
+
},1000);
|
162
|
+
|
163
|
+
});
|
164
|
+
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
const wait=async()=>{
|
170
|
+
|
171
|
+
console.log(2.1);
|
172
|
+
|
173
|
+
console.log(2.2);
|
174
|
+
|
175
|
+
await console.log(2.3);
|
176
|
+
|
177
|
+
console.log(2.4);
|
178
|
+
|
179
|
+
console.log(2.5);
|
180
|
+
|
181
|
+
let a = await asyncFunc().then(val=>{
|
182
|
+
|
183
|
+
console.log(val);
|
184
|
+
|
185
|
+
return val + " likes";
|
186
|
+
|
187
|
+
});
|
188
|
+
|
189
|
+
console.log(a);
|
190
|
+
|
191
|
+
console.log(5);
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
console.log(1);
|
198
|
+
|
199
|
+
wait();
|
200
|
+
|
201
|
+
console.log(4.1);
|
202
|
+
|
203
|
+
for(var i=0;i<999999999;i++){} //同期処理で重めのループ
|
204
|
+
|
205
|
+
console.log(4.2);
|
206
|
+
|
207
|
+
```
|
208
|
+
|
209
|
+
結果:
|
210
|
+
|
211
|
+
1
|
212
|
+
|
213
|
+
2.1
|
214
|
+
|
215
|
+
2.2
|
216
|
+
|
217
|
+
2.3 //ここでawaitがかかるので処理が移り
|
218
|
+
|
219
|
+
4.1
|
220
|
+
|
221
|
+
(ちょっと考え込んで)
|
222
|
+
|
223
|
+
4.2 //同期処理が続くので終了まで待ち
|
224
|
+
|
225
|
+
2.4 //元の処理にもど
|
226
|
+
|
227
|
+
2.5
|
228
|
+
|
229
|
+
(1秒待って)
|
230
|
+
|
231
|
+
3
|
232
|
+
|
233
|
+
he
|
234
|
+
|
235
|
+
he likes
|
236
|
+
|
237
|
+
5
|