-
-
Save hsbalar/f08589819613da13481798f40e221af8 to your computer and use it in GitHub Desktop.
Async job in custom order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
H ~~~~ -> | |
A -> B -> | |
C -> D | |
H: high priority long task, should start in parallel to A | |
A: first task | |
B: should start once A is finished | |
C: should start once B & H is finished | |
D: should start once C is finished | |
*/ | |
function task(x,sec) { | |
return new Promise(resolve => { | |
console.log('Start: ' + x); | |
setTimeout(() => { | |
console.log('End: ' + x); | |
resolve(x); | |
}, sec *1000); | |
}); | |
} | |
async function run() { | |
h = task('H', 5); | |
a = task('A',1); | |
b = await a && task('B', 1); | |
c = await h && b && task('C', 1); | |
await c && task('D', 1); | |
} | |
run(); | |
/* OUTPUT | |
Start: H | |
Start: A | |
{ [[PromiseStatus]]:pending, [[PromiseValue]]:undefined } | |
End: A | |
Start: B | |
End: B | |
End: H | |
Start: C | |
End: C | |
Start: D | |
End: D | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment