Live data from Hacker News

I Avoid Async/Await

uniqname.medium.com

11–20 of 242 posts

Re: I Avoid Async/Await

#11
I kind of agree. I learned promises from the promise API - flatmapping over things, that will happen when the thing is resolved.

I remember async/await was harder to grok because it seemed so arbitrary. A bunch of syntax sugar, and function decorations.. promises were just method calls.

Re: I Avoid Async/Await

#12

Async / await is nice syntactic sugar. I don't understand why they copied the naming from c# over do syntax in Haskell (which dates roughly 1995, if I'm not mistaken). The main problem is error handling, having to use try / catch is pure cancer and really screw up your closure. You can use catch together with await / async but then you're not dealing with exceptions outside the promises.

async/await is not the same as the do-notation, that's why.

Re: I Avoid Async/Await

#13
Regarding the error handling case, I've played around with the idea of implementing a Result type with typescript and hiding the errors behind that. The end result looks something like this

    const res = await getJSON('/thingy');

    if (isErr(res)) {
      // Handle error.
      return;
    }
You can read the details from here: https://thingsthatkeepmeupatnight.dev/posts/simple-typescrip...

Re: I Avoid Async/Await

#14
I try to read every one of these sorts of articles that comes up. I don't want to be caught flat-footed on some kind of weird memory leak issue from not using a pattern correctly, or something.

But every single one of these articles that I've read against async/await has been nothing more than inventing "problems" to try to convince people to stick to raw promises strictly because that's what the author is comfortable with using. No actually technical merits are discussed. Just fantasy issues so author can feel superior for not learning something new.

Like how the author claims you can't visually "see" that two await calls can be parallelized. Speak for yourself, buddy. And then complains about "having to go back to using promises" to affect the parallelization. My dude, it was promises all along.

It's just syntax. Use it, don't use it, mix and match it. It's not a moral issue.

Re: I Avoid Async/Await

#15
> It’s because we are taught to read async/await code in a synchronous mindset. We can’t parallelize the save calls in that first fully synchronous example, and that same — but now incorrect — logic follows us to the second example.

What a weird idea. When I see a serial bunch of awaits the first I think of is whether that makes any sense.

I think the author is kind of projecting his own views on everyone else here.

Re: I Avoid Async/Await

#16
YOU may, buy your core frameworks may not. And if your synchronous code is holding up async loops, you're gonna potentially create a lot of very hard to diagnose problems. Beware the foundation on which you build.

Re: I Avoid Async/Await

#17

I try to read every one of these sorts of articles that comes up. I don't want to be caught flat-footed on some kind of weird memory leak issue from not using a pattern correctly, or something. But every single one of these articles that I've read against async/await has been nothing more than inventing "problems" to try to convince people to stick to raw promises strictly because that's what the author is comfortabl…

Yup, I agree that the author is missing the point here. You can mix these syntaxes and have, say, "await Promise.all" without guilt.

Re: I Avoid Async/Await

#18
I always wondered why async/await introduced a concise way of dealing with asynchronous code, but then breaks all this conciseness with try/catch ...

Hence I tried to combine async/await with catch for an improved readability [0] two years ago. Turns out I never used this approach, because it's not common sense when working in a team and still feels too verbose. Either one uses then/catch or async/await with try/catch. However, I still feel try/catch still makes code unattractive.

[0] https://www.robinwieruch.de/javascript-async-await-without-t...

Re: I Avoid Async/Await

#19

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…

> Being able to store tasks/promises and reuse/await them later is a clear advantage

This sounds like horrible spaghetti. I can understand you might need to do it in exceptional circumstances, but I wouldn’t make a habit of it.

Re: I Avoid Async/Await

#20

I try to read every one of these sorts of articles that comes up. I don't want to be caught flat-footed on some kind of weird memory leak issue from not using a pattern correctly, or something. But every single one of these articles that I've read against async/await has been nothing more than inventing "problems" to try to convince people to stick to raw promises strictly because that's what the author is comfortabl…

> you can't visually "see" that two await calls can be parallelized. Speak for yourself, buddy.

Agreed. My first thought was to try

  var userSaveTask = save('userData', userData);
  var sessionSaveTask = save('session', sessionPrefences);
  await Task.WhenAll(userSaveTask, sessionSaveTask);

I saw it before I got to the part of the text explaining how I wasn't going to just see it.

> Furthermore, if we are to take advantage of parallelization in the async/await example, we must use promises anyway.

Again, Speak for yourself, buddy. "await Task.WhenAll" does that job in c#, and there must be a JavaScript equivalent.

IDK, this may be the c# mindset. yes, "async / await" is an extra-ordinarily complex feature that can easily be done wrong. But its also very useful and powerful, so lets not throw it out.

Post reply on HN