Created
July 18, 2018 16:05
-
-
Save chinoto/7a9aa7649c6607d6e06faddb43cef2f7 to your computer and use it in GitHub Desktop.
Demonstrating different ways of using promises with(out) async/await that are dependent on multiple promises
This file contains 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
const sleep=(fn,ms)=>new Promise(r=>setTimeout(()=>r(fn()),ms)); | |
const init=Date.now(); | |
const log=(y)=>console.log((Date.now()-init)+' '+y); | |
//7: Awaiting on required values immediately. Very slow. | |
(async () => { | |
const x=await sleep(()=>1,100); | |
const a=await sleep(()=>3,200); | |
const b=await sleep(()=>4,300); | |
const ax=await sleep(()=>a*x**2,1000); | |
const bx=await sleep(()=>b*x,500); | |
return ax + bx; | |
})().then(log); | |
/*20: Initialize promises, then await. This is faster, but ax_p can't run until | |
b_p resolves. We could await b_p after ax_p because ax_p doesn't need b_p and | |
we know b_p will take longer to resolve since we can see the timings, but in | |
real world scenarios, timings are unpredictable.*/ | |
(async () => { | |
const x_p=sleep(()=>2,100); | |
const a_p=sleep(()=>3,200); | |
const b_p=sleep(()=>4,300); | |
const x=await x_p; | |
const a=await a_p; | |
const b=await b_p; | |
const ax_p=sleep(()=>a*x**2,1000); | |
const bx_p=sleep(()=>b*x,500); | |
return await ax_p + await bx_p; | |
})().then(log); | |
//39: Same as before, but with Promise.all(). | |
(async () => { | |
const x_p=sleep(()=>3,100); | |
const a_p=sleep(()=>3,200); | |
const b_p=sleep(()=>4,300); | |
const [x,a,b]=await Promise.all([x_p,a_p,b_p]); | |
const [ax,bx]=await Promise.all([ | |
sleep(()=>a*x**2,1000), | |
sleep(()=>b*x,500) | |
]); | |
return ax + bx; | |
})().then(log); | |
//64: Use nested async/await to only await the values needed for each result. | |
(async () => { | |
const x_p=sleep(()=>4,100); | |
const a_p=sleep(()=>3,200); | |
const b_p=sleep(()=>4,300); | |
const ax_p=(async ()=>{ | |
const a=await a_p; | |
const x=await x_p; | |
return await sleep(()=>a*x**2,1000); | |
})(); | |
const bx_p=(async ()=>{ | |
const b=await b_p; | |
const x=await x_p; | |
return await sleep(()=>b*x,500); | |
})(); | |
return await ax_p + await bx_p; | |
})().then(log); | |
//95: Same as before, but with Promise.all(). | |
(async () => { | |
const x_p=sleep(()=>5,100); | |
const a_p=sleep(()=>3,200); | |
const b_p=sleep(()=>4,300); | |
const [ax,bx]=await Promise.all([ | |
(async ()=>{ | |
const [a,x]=await Promise.all([a_p,x_p]); | |
return await sleep(()=>a*x**2,1000); | |
})(), | |
(async ()=>{ | |
const [b,x]=await Promise.all([b_p,x_p]); | |
return await sleep(()=>b*x,500); | |
})() | |
]); | |
return ax + bx; | |
})().then(log); | |
//132: Same as before, but without any async/await, just raw promises. | |
(() => { | |
const x_p=sleep(()=>6,100); | |
const a_p=sleep(()=>3,200); | |
const b_p=sleep(()=>4,300); | |
const ax_p=Promise.all([a_p,x_p]).then(([a,x])=>sleep(()=>a*x**2,1000)); | |
const bx_p=Promise.all([b_p,x_p]).then(([b,x])=>sleep(()=>b*x,500)); | |
return Promise.all([ax_p,bx_p]).then(([ax,bx])=>ax+bx); | |
})().then(log); | |
/* | |
End result: | |
1203 132 | |
1205 64 | |
1205 95 | |
1304 20 | |
1304 39 | |
2104 7 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment