4 ms·
Instead of using Promise.all, wouldn't an approach like this work? const a = taskA(); const b = taskB(); const c = taskC(); const result = [aw
by petepete 9y ago
Instead of using Promise.all, wouldn't an approach like this work?
const a = taskA();
const b = taskB();
const c = taskC();
const result = [await a, await b, await c];
- cropsieboss 9y agoI believe `await` is sequential in the scope. `Promise.all` would be truly parallel. edit: just checked and your version is indeed parallel. Interesting.
- dvlsg 9y agoYup - all promises are "started" before you await them. I still typically prefer Promise.all though. It's a bit more obvious what's going on (IMO) and you can skip the additional lines for assigning the promises to variables.
- styfle 9y agoVery cool, I never thought about doing it this way!
- itwy 9y agoThese are not the same. Promise.all guarantees a is executed then b then c. Your version guarantees the three are executed before moving forward. The order may differ on each run.
- exogen 9y agoFYI, Promise.all does not guarantee that. It doesn't orchestrate actually running the Promises at all (this is impossible the way Promises work), just that they're all resolved at the end, with results in the same order they were passed in.
- itwy 9y agoWhat is the difference then?
- hdhzy 9y agoNone from the perspective of the consumer. The implementation of Promise.all could use one array for results and fill it in any order as soon as any promise resolves while awaits will always proceed in strict order (like a generator). But that's an implementation detail that a JS programmer doesn't need to think about.
- itwy 9y agoThanks for the clarification.