Re:JavaScript best practice: use return await
by mdc2161 on Friday March 07, 2025 @05:39PM
(#43295580)
It is very much not being turned into synchronous code. There are a lot of reasons to use async code, and they're all still valid with this pattern.
For example:
- the event loop is still not being blocked
- the calling code can still be flat instead of nested callbacks (truly synchronous) or chained `.then` calls
Is it a little bit more boilerplate? Yes, definitely, and it's certainly less nice to look at. But it's linter-enforceable consistency that removes the cognitive overhead of an annoying to debug footgun.
Note: My favorite style is typed error returns instead of thrown exceptions. In that world, you only need try/catches at the boundaries of your code and for application crashing exceptions. Unfortunately most codebases aren't written like that and I'm often working in inherited code instead of greenfield.
Re:JavaScript best practice: use return await
by beardyw on Saturday March 08, 2025 @10:13AM
(#43300846)
I think my point is that awaits are often used because the coder can't work out or can't be bothered to work out how to treat promises more intelligently.
For example:
- the event loop is still not being blocked
- the calling code can still be flat instead of nested callbacks (truly synchronous) or chained `.then` calls
Is it a little bit more boilerplate? Yes, definitely, and it's certainly less nice to look at. But it's linter-enforceable consistency that removes the cognitive overhead of an annoying to debug footgun.
Note: My favorite style is typed error returns instead of thrown exceptions. In that world, you only need try/catches at the boundaries of your code and for application crashing exceptions. Unfortunately most codebases aren't written like that and I'm often working in inherited code instead of greenfield.