Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I wish the await syntax was inverted; instead of waiting for an async function, let all async functions await by default. In other words, I wish this worked:

    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.



> 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.

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()`.


I wish your comment gets upvoted to national news. Those async antipatterns are worse than the 'goto' statement. Time for programming languages to add the 'async' keyword on the caller side.


JavaScript often feels too verbose. That's why I like to use elm whenever it is possible.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: