Last active
May 7, 2021 16:32
-
-
Save vijayjangid/affe20ac7e01d2f0875b4714b4ce239a 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 | |
*/ |
/*
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
*/
Rectify problem question
Done. thanks for pointing it out.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/*
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
*/
Rectify problem question