Skip to content

Instantly share code, notes, and snippets.

@vijayjangid
Last active May 7, 2021 16:32
Show Gist options
  • Save vijayjangid/affe20ac7e01d2f0875b4714b4ce239a to your computer and use it in GitHub Desktop.
Save vijayjangid/affe20ac7e01d2f0875b4714b4ce239a to your computer and use it in GitHub Desktop.
Async job in custom order
/*
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
*/
@hsbalar
Copy link

hsbalar commented Feb 16, 2021

/*
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

@vijayjangid
Copy link
Author

/*
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