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 }));
Good point! Never thought about it.
You have a good point. Actually, depending on the context, each approach may offer distinct advantages.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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:
Good point! Never thought about it.
You have a good point. Actually, depending on the context, each approach may offer distinct advantages.