I am reading up more on Asynchronous Codes - and in this https://developers.google.com/web/fundamentals/primers/async-functions by Google, I realise that that if I add await to every line of code, sometimes it takes longer to process due to the code running in Series rather than Parallel.
Here's the 2 code samples provided on that page.
async function series() {
await wait(500); // Wait 500ms…
await wait(500); // …then wait another 500ms.
return "done!";
}
async function parallel() {
const wait1 = wait(500); // Start a 500ms timer asynchronously…
const wait2 = wait(500); // …meaning this timer happens in parallel.
await wait1; // Wait 500ms for the first timer…
await wait2; // …by which time this timer has already finished.
return "done!";
}
Can I understand as both codes look similar, as in they use await
on the function wait1
and wait 2
. What makes one parallel while the other in series?