Closed fuunnx closed 1 year ago
I mean sure, that particular example wouldn't be helped, because you made the mistake of awaiting them first - iow, it's not actually a promise-valued object, so Promise.ownProperties
of it is useless.
There's no possible way to make those be equivalent. If your code asks to await
something, it must be so.
I agree with @ljharb here. Adding this as an under-the-hood optimization would actually backfire, because
foo()
could depend on bar()
, which wouldn't work in existing codeLet me give another example which recently bit me about the await keyword:
await foo ? bar() : baz()
Naively, I thought the await would apply to bar
or baz
, but it applied to foo
. In hindsight, I can see why. In practice, this bug led NodeJS to exit out from under me with no obvious reason. NodeJS's behavior was correct, if you think about "what is the await keyword closest to?".
Naively I think this example could be optimized away by the compiler with an internal
await Promise.ownProperties()
because there would be no semantic change between chaining or parallelizing them :No need for new constructs and it would be a performance win for old usages