4 ms·
The way I would write the code is never covered, and I can't tell it's because I don't understand async, or I'm secretly a genius. The article says this is cor
by recursive 6y ago
The way I would write the code is never covered, and I can't tell it's because I don't understand async, or I'm secretly a genius.
The article says this is correct.
> const both = await Promise.all([ members, nonMembers ]);
> return both[0].concat(both[1]);
I would have written it this way without the third promise.
> return (await members).concat(await nonMembers);
Is this wrong somehow?
- ajkjk 6y agoAfaik that just runs in serial, like the original example. `await` pauses execution immediately.
- recursive 6y agoYes, but the first pause comes after both requests have been started.
- ajkjk 6y agoAre you sure? It seems incongruous with my mental model, which is that the first `await`, when executed, immediately pauses execution, so the second is only dispatched when the first has resolved.
- recursive 6y agoYes. > the first `await`, when executed, immediately pauses execution Also true, but in my code, the first `await` happens after the second `fetch()` has already begun.
- DangitBobby 6y agoThe second await does not start until the first await resolves the first promise, but that doesn't mean network activity for the second initiated request isn't still happening in the background.
- DangitBobby 6y agoIn all examples provided, unless you want to return them as separate promises, you must await at some point. My understanding is that calling await on a promise does not prevent other Promises from being fulfilled, it just pauses execution of the rest of the code block. For example, if you performed two web requests and then awaited a setTimeout for a few seconds, your two web request promises would likely already be resolved by the time you awaited them. Which would mean that the difference between await fetch await fetch concat and fetch fetch concat await is that the fetch can actually perform asynchronous web activity in the latter example.
- nateroling 6y agoThis has the same problem, (await members) will happen first, only when that’s done will (await nonMembers) happen.
- recursive 6y agoThat seems different, and not like a problem at all. My understanding is that `await` refers to the resolution of a promise, not its start.
- azangru 6y agoIsn't it as sequential as the original code?
- recursive 6y agoI don't believe so. Both promises are created before any are awaited.
- nine_k 6y agoThis is not wrong, it's just not parallel. That is, the second promise, inside `.concat()`, will only get created and scheduled after the `await members` will have resolved. The point of `Promise.all` is to have every promise passed to it to start computing concurrently, likely in parallel if they e.g. start independent I/O operations.
- phiresky 6y agoThis is not true, since in JS promises are dispatched as soon as they are created. So as long as you call the fetch function, it doesn't matter that you are calling `await` sequentially.
- ctidd 6y agoIf I follow your statement, it doesn't appear correct. JS Promises are eager, and the work underlying the promise will begin executing prior to being awaited. Await is just blocking on that execution's resolution. // 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.
- faizshah 6y agoThis is true and this is the root cause of the misunderstanding from the blog. Promises execute immediately, await blocks on a promise. Promise.all and Promise.allSettled allow you to compose promises and block on a single promise instead of multiple awaits but have nothing to do with executing a promise. 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.
- nine_k 6y agoI can't get it, and from my experience, this is not true. Consider: const x = await foo(); await bar(x); How can you schedule `foo()` and `bar()` in parallel, when they have an explicit data dependency?