by atombender · 9y ago · 0 comments
let foo = myAsyncFunc()
foo.bar()
Why? Because that's the common case, and the caller shouldn't care whether the function is async or not. If you have a non-async function that you want to change to be async, or the other way around, then you have to change every single call site.The case where you want to store a promise or wait on multiple is actually the edge case, and they could have reused "async" here:
Promise.all([
async myAsyncFunc(),
async someOtherAsyncFunc()
]);
Bonus functionality: If you do "async foo()" and foo() isn't async, the compiler could still ensure that it provided a promise.Sadly, it's too late for this, and everyone's code will suffer as a result. It's way too easy to forget to add "await" to an async call, and doing so leads to failures that can be hard to track down.
At least in the direction non-async -> async, that is clearly a good thing, since you are changing semantics a lot.
This code is always safe:
x = globalObject.a
functionWithoutSideEffects()
gobalObject.a = x + 1
While this is not: x = globalObject.a
await functionWithoutSideEffects()
gobalObject.a = x + 1
> It's way too easy to forget to add "await" to an async call, and doing so leads to failures that can be hard to track down.This a situation that could clearly be improved with better detection of these cases. Or maybe it should be completely illegal to call an async function without a keyword, so you have to do either `await func()` or `promise func()`.