DEV Community

Discussion on: Handling Asynchronous Errors Like a Pro

Collapse
 
atscub profile image
Abraham Toledo

Interesting post. Personally, I prefer the try catch approach. I try to avoid the promise.then syntax since it can be hard to understand the execution flow.

Collapse
 
lexlohr profile image
Alex Lohr

I prefer a mix of both await and .then/.catch, since try-catch is usually outside of the promise chain, so you cannot pinpoint where the error happened unless you wrap every single promise in a try-catch-block, which breaks the reading flow:

const data = await fetch(url)
  .catch((error) => ({ msg: "request failed", error })
  .then(r => r.json())
  .catch((error) => ({ msg: "request malformed", error }));
Enter fullscreen mode Exit fullscreen mode
Collapse
 
atscub profile image
Abraham Toledo

Good point! Never thought about it.

Collapse
 
lisichaviano profile image
Lisandra

You have a good point. Actually, depending on the context, each approach may offer distinct advantages.