Live data from Hacker News

I Avoid Async/Await

uniqname.medium.com

31–40 of 242 posts

Re: I Avoid Async/Await

#32

First off, async/await is promises. It's merely syntactic sugar. The point of async/await is not to never have the word "Promise" appear in your code. It's also not meant to be universally better than using Promises bare-bones. A lot of the argument appears to be the author extrapolating from his own lack of familiarity to others: "We are taught", "our minds", etc. I can easily construe some hypothetical person with…

This, very much this.

    async function() {}
is much nicer than

    function() {
      return new Promise(
        (resolve) => resolve()
      )
    }

Re: I Avoid Async/Await

#34
post #10

As a fullstack dev, I worked with - Spring MVC (Java Futures) - Spring Webflux (Reactor observables) - Scala (Scala Futures & for comprehensions) - TS async/await - Angular observables - React hooks (which combined with Redux, React Query etc. is a special approach to async programming.) I think it's very important to understand that ultimately, async (non-blocking) programming is kind of a "hard problem" and there i…

Try ou kotlin coroutines. The suspend keyword really helps out reasoning.

Re: I Avoid Async/Await

#35
post #10

As a fullstack dev, I worked with - Spring MVC (Java Futures) - Spring Webflux (Reactor observables) - Scala (Scala Futures & for comprehensions) - TS async/await - Angular observables - React hooks (which combined with Redux, React Query etc. is a special approach to async programming.) I think it's very important to understand that ultimately, async (non-blocking) programming is kind of a "hard problem" and there i…

"[..] and the JVM has limited thread count, too"

I think I understand you point when it comes to the essentially single threaded Javascript runtime, but the thread limit of the JVM is huge, so how does that matter?

Not trying to be pedantic, just wanted to know if I missed something.

Re: I Avoid Async/Await

#36
> Give better cues that we are in an asynchronous mental model

The `async` keyword(!) is objectively a clearer signal that the code in question is asynchronous. That's why type-checkers use it to prevent you from doing dumb stuff like awaiting in a synchronous function.

> In simple cases express the code at least as cleanly as async/await

It's pretty hard to see much of an argument here. How can the promise version ever be seen as "at least as clean"?

    await someTask()
    // vs
    someTask().then(() => ...);
Even beyond the syntax here, the promise forces you into at least one level deep of nesting, which instantly makes it much trickier to manage intermediate variables, which either need to be passed along as part of the result (usually resulting in an unwieldy blob of data) or the promises need to be nested (pyramid of doom) so that inner callbacks can access the results from outer promises.

> Provides a much cleaner option for more complex workflows that include error handling and parallelisation.

If you've somehow come to the conclusion that `Promise.all` doesn't work with `async/await` then you have probably misunderstood the relationship between `async` functions and promises. They're the same thing. Want to parallelise a bunch of `await` statements? You can still use `Promise.all`!

I do occasionally find try-catch to be awkward, but that's because it creates a new lexical scope (just like promise callbacks do). I also think the consistency from having one unified way to handle errors in sync/async contexts justifies it.

Re: I Avoid Async/Await

#37
the whole blog-post seems a little constructed tbh - If I look at await doSomething(param) await doSomethingElse(param)

the very first thing that comes to mind is that this can be very, very easily optimised by just doing await Promise.all([ doSomething(param), doSomethingElse(param) ])

This is something I come across literally every day - why exactly would that be an argument against async/await? much so on the contrary, I find const myResult = await Promise.all([]) significantly more expressive, than Promise.all([]).then(doSomething)

Also, as others have already pointed out - async/await is just syntactic sugar around promises and can be used in addition to 'normal' promise syntax (as shown above)

Re: I Avoid Async/Await

#38
All async/await is built on promises/futures/tasks, which themselves are built on yield/generators. Most modern languages have converged on this and they are not different paradigms but rather just newer syntax.

Async/await is better for expressing concurrent logic more tersely and in the common "sync" format, and failure to understand what it means is just that, a failure in understanding.

There's no convincing argument here to use "promises instead async" because it's the same thing.

Re: I Avoid Async/Await

#39
Now maybe it’s just my familiarity with Promises, but I look at the third example and I can quickly see an opportunity.

This entire article is built around the author's ignorance and could easily be summarised as "I avoid async/await syntax because I'm more familiar with promises". The author doesn't even appear to understand that async/await is syntactic sugar for promises.

Re: I Avoid Async/Await

#40

If you're doing nothing in between your async calls, using .then/.catch might be simpler. As soon as you need to introduce local variables and complex control structures, not having shared closures between your .then methods becomes extremely limiting. Hence, the async/await sugar. About having to add await to ensure your error is handled with the try catch — not putting an await before a promise is something I use a…

Completely my feelings too. He doesn't actually address this, and it is actually the really painful part about promises which async/await makes infinitely better.

I have had cases where converting .then(...) code to async/await made the code infinitely easier to understand/reason about and made it trivial remove bugs which were present due to the complexities of dealing with control flow logic.

It strikes me that the author is just already "used to" promises and as such is trying to justify their preference for them with examples which they believe "prove" that promises are "better", but actually fail to do so. For every single one of their examples, async/await is no worse, if not better.

For example, their argument about inadvertently serialized work with the the two save calls. They are claiming that ".then()" is more obviously serializing than the "await" keyword, which is a highly subjective claim. If there is a problem here, it's that some developers don't understand/know about all the asynchronous tools which are available and so are unaware of the option of using Promises.all(...).

Another argument for async/await compared to promises is the following:

  try
  {
    await doSomething();
  }
  catch()
  {
    // Do a particular error handling for doSomething() failing
  }
  
  try
  {
    await doSomethingElse();
  }
  catch()
  {
    // Do a different particular error handling for doSomethingElse() failing
  }
What's the best way to reproduce this in promises only land, for example, does the following work?

  doSomething()
    .catch(() => {
      // Do a particular error handling for doSomething() failing
    })
    .then(() => doSomethingElse())
    .catch(() => {
      // Do a different particular error handling for doSomethingElse() failing
    });
I think it works, but I honestly don't know for sure offhand and to be sure I would need to check the documentation for promises, whereas for the async/await approach, there is no question. And if the above doesn't work, then I would have to call the .then() from inside the first .catch() block, which is awful code to read and interpret.
Post reply on HN