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
37
38
39
40
function step1(query) {
return new Promise((success) => {
let waiting=2000;
setTimeout(() => {
success('wait-time:'+waiting+' step:'+query);
}, waiting);
});
}
function step2(query) {
return new Promise((success) => {
let waiting=1000;
setTimeout(() => {
success('wait-time:'+waiting+' step:'+query);
}, waiting);
});
}
function step3(query) {
return new Promise((success) => {
let waiting=500;
setTimeout(() => {
success('wait-time:'+waiting+' step:'+query);
}, waiting);
});
}

const task = async()=>{
await step1('step1').then((success) => {
console.log(success);
});

await step2('step2').then((success) => {
console.log(success);
});

await step3('step3').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