News for hackers. Stuff that matters.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by ohvirginia on Friday May 08, 2020 @09:33AM (#23114670)
Except for the return to happen when that's true, you'd need an await. At least in JS.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by acarl005 on Friday May 08, 2020 @09:39AM (#23114732)
Not if it's returned by an async function. In JS, async functions automatically await promises that are returned.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by emilecantin on Friday May 08, 2020 @10:50AM (#23115385)
That's not actually right. An async function will always return a promise, so if you return a promise directly it's indeed pretty much the same as if you return a promise that waits on that promise; it's the calling function that actually awaits on the returned promise.

The main difference would be error handling: if you have a catch block that returns a different value, returning the promise directly would actually throw in the calling function, whereas if you "return await", it's going to get caught in your catch block, resolving the promise to your own custom value.

Found a detailed explanation here: https://jakearchibald.com/2017/await-vs-return-vs-return-awa...