https://medium.com/young-coder/5-misconceptions-about-asynchronous-code-in-javascript-ebad2738766


Promises run when created, not awaited.
You could solve the problem by getting each promise and combining them into one with Promise.all().
But that’s not even necessary. All you need to do is let each Promise object be created, at which point the code begins running. If you do that, it doesn’t matter what order you wait on them, because they’re all executing their code in parallel:

const promise1 = slowFunction(dataObject1);
const promise2 = slowFunction(dataObject2);
const promise3 = slowFunction(dataObject3);
const response1 = await promise1;
const response2 = await promise2;
const response3 = await promise3;

0 commenti

Lascia un commento

Segnaposto per l'avatar