avatar

Catalog
async await promise demo

example video

Javascript
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
function step1(query) {
return new Promise((success) => {
setTimeout(() => {
success(query);
}, 2000);
});
}
function step2(query) {
return new Promise((success) => {
setTimeout(() => {
success(query);
}, 1000);
});
}
function step3(query) {
return new Promise((success) => {
setTimeout(() => {
success(query);
}, 500);
});
}
const task = async()=>{
await step1(1).then((success) => {
console.log(success);
});;

await step1(2).then((success) => {
console.log(success);
});;

return step1(3).then(success => {
console.log(success);
});
}
task();
// step 1,2,3 setTimout'time is decrement,but will execute in order by async function tack
Author: Qin
Link: https://qinhaolei.com/posts/14797/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Reward
  • WechatPay
    WechatPay
  • AliPay
    AliPay
  • Bitcoin
    Bitcoin

Comment