by ctidd · 6y ago · 0 comments
// Given:
const fooPromise = asyncFoo();
const barPromise = asyncBar();
// Then this...
await fooPromise;
await barPromise;
// ...is equivalent to this:
await Promise.all([fooPromise, barPromise]);
Now, Promise.all() is useful, but it is unrelated to when computation starts.The misunderstanding in the article has to do with misunderstanding when a promise is executed. It’s easy to wonder why calling await on a promise directly vs calling it on a variable containing a promise is different. The difference is promises execute immediately on creation while await blocks on a promise. So a promise that is created after an await will be executed after that awaited promise is resolved.
Consider:
const x = await foo();
await bar(x);
How can you schedule `foo()` and `bar()` in parallel, when they have an explicit data dependency?You can't, but that's a fundamentally different scenario than what's being discussed.