News for hackers. Stuff that matters.
Re:Beware of Async/Await by brundolf on Thursday July 30, 2020 @01:01PM (#24000472)
I think many newer developers, especially ones who are naive to asynchronous programming, may learn/be taught that "when you want to get a result from an API, you just need to put 'await' in front of it", without fully understanding what exactly it entails. There may be fewer people in that situation on HN than there are in the developer population at large, but I theorize that it's a widespread issue and I wanted to shed some light on it.
Re:Beware of Async/Await by TheCoelacanth on Thursday July 30, 2020 @01:06PM (#24000532)
That's true, but in my experience, they also just memorize the ".then(result =>" pattern and make exactly the same mistake.
Re:Beware of Async/Await by brundolf on Thursday July 30, 2020 @01:26PM (#24000768)
But I maintain that in the illustrated case - where you want to get all the different independent values and then combine them at the end - it's actually less ergonomic to do it the wrong way with .then(...) than it is to do it the right way.
Re:Beware of Async/Await by jakear on Thursday July 30, 2020 @01:34PM (#24000852)
Most ergonomic IMO is:

const getPeople = async () => (await Promise.all(...)).flat()

Without await this would be

const getPeople = () => Promise.all(...).then(arrs => arrs.flat())

The difference is slight to be sure, but I usually prefer the await approach.