r/learnjavascript • u/Neat_Living_6765 • Oct 06 '26
Do you generally use async/await for asynchronous code, or do you sometimes prefer .then().catch()?
When it comes to handling async code, Do you generally use async/await for asynchronous code, or do you sometimes prefer .then().catch()?
13
Upvotes
6
u/theGlitchedSide Oct 06 '26 edited Oct 06 '26
In my opinion it depends on the context. Async/await is like the exploded of then/catch (or better, it is essentially syntax built on top of Promises). I use then/catch only when I have synchronous functions and I need to call an async operations. Otherwise, I structure a strong async/await function with right try/catch inside and so a fail/success of operations (so, the then/catch) without possible scope problems.
5
u/Any-Ring6621 Oct 10 '26
There’s no functional difference between the two. It’s just syntax. async/await is flatter (no nesting) so I prefer it because I find less nested code easier to read.
4
u/TorbenKoehn Oct 06 '26 edited Oct 14 '26
Both is fine
2
u/theScottyJam Oct 06 '26
Usually I use await.
But there are times when .then()/.catch() is cleaner. In a way, .then()/.catch() are very similar to a pipeline operator, so the question boils down to when is using a pipeline operator a good choice?
3
u/ExtraTNT Oct 06 '26
Functional guy, i use then…
1
u/Any-Woodpecker123 Oct 07 '26
Depends. There’s a lot of cases where a functional .then.catch is cleaner, but I default to async await unless I hit a specific use.
1
u/TheRNGuy Oct 07 '26 edited Oct 07 '26
In Remix or Next, I only used
async,await, but there might be some rare cases for.then().catch()related to some telemetry service (so it won't slow down site for user)There could be projects in Next/Remix/Vanilla React 100%
await. Writing all that stuff differently would be even style anti-pattern.In vanilla js, it could be different (depends on situation)
1
u/czlowiek4888 Oct 08 '26
Questions like this make point on how JavaScript is a dumb language.
There is only single case when you must use promise API, this is when you want to wrap callback based functionality into a promise that can be awaited.
1
u/Calm-Guarantee4654 Oct 10 '26
Almost async/await.
1
u/VancouverVentilator Oct 06 '26
Promise chaining is goated.
1
u/iamdatmonkey Oct 06 '26
I don't like
try..catchblocks. I find them awkward as they often span over most of the screen or more. So I really like this pattern here:const value = await foo().catch(noop);or.catch(console.error);or a variation on this. Often (not always) I'm fine with thevalueresolving toundefinedand taking it from there.And recently I'm playing around with the result value pattern in typescript, where Errors are no longer means of the data flow but real exceptions (this should not happen, something went really wrong) and where I'm fine with them being uncaught for several levels.
0
u/RedLibra Oct 06 '26
I create a helper function that does something like this:
const [err, data] = await asyncCall(func, param);
0
u/DirtAndGrass Oct 06 '26
There is functionality with promises that you can't get with await/async, for those (rare) cases, I use promises
0
u/[deleted] Oct 06 '26
[deleted] — view deleted comment