2 ms·
May understanding can as well be wrong! I expected `await X` to create the promise `X` when it starts executing, much like `cond_A || cond_B` only computes `co
by nine_k 6y ago
May understanding can as well be wrong!
I expected `await X` to create the promise `X` when it starts executing, much like `cond_A || cond_B` only computes `cond_B` when `cond_A` has been computed and turned out falsy.
Else how the following code ever work correctly?
const x = await foo();
const y = await bar(x);
We cannot schedule `bar()` before we have computed its inputs.
OTOH `(await x()).concat(await y())` could speculatively compute both promises concurrently, even if it does not yet know whether the result of `x()` has a property named `concat`. I won't normally expect it, but JS is used to defy logic in many areas :-\
- stormbrew 6y agono, generally `await` is the fulfilment of a promise already created. `foo()` returns a promise, and then the await triggers the ceding of control to the event loop and allows the promise to progress. In the example you've shown, it is absolutely correct that the two promises must be sequentially awaited like that. The OP is talking about a situation where there is no dependency between x and y.