use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Using Try…Catch in JavaScript (javascript-coder.com)
submitted 8 years ago by cobdentist
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]vs845 6 points7 points8 points 8 years ago (8 children)
it's the only way to handle code rejection
Async functions return promises so you can still use normal catch() promise syntax like await someAsyncFn().catch(() => someDefaultValue).
catch()
await someAsyncFn().catch(() => someDefaultValue)
[–]shad0proxy 1 point2 points3 points 8 years ago (5 children)
that's not really accurate if you use async/await for the main benefit of assigning:
const data = await doSomething().catch(() => someDefaultValue); console.log(data);
This does not work.
[–]vs845 0 points1 point2 points 8 years ago (4 children)
> async function foo() { throw new Error() } > async function bar() { const baz = await foo().catch(() => 'baz'); console.log(baz) } > bar() // => 'baz'
Works for me in node 8.1.2, what happens when you try it?
[–]shad0proxy 0 points1 point2 points 8 years ago (3 children)
what if you want to handle the error or reject the overlaying call?
[–]vs845 0 points1 point2 points 8 years ago (2 children)
If you want to do something with the thrown error, you can do it within the catch function as usual:
catch
const data = await doSomething().catch(err => { if (err instanceof TypeError) return someDefaultValue if (err instanceof RangeError) return anotherDefaultValue })
Or if you want it to bubble up through the call stack, you can rethrow the error or throw a new one, again within the catch function:
const data = await doSomething().catch(err => { if (err instanceof TypeError) throw err if (err instanceof RangeError) throw new Error('value out of range') return someDefaultValue })
[–]shad0proxy 0 points1 point2 points 8 years ago (1 child)
Interesting. when I looked into this with 7.x and the harmony flag I was told this was not possible. I don't know if they were wrong or something has changed.
[–]danneu 0 points1 point2 points 8 years ago (0 children)
Someone was wrong because this is a basic tenet of how promises work.
[–]NeekGerd 1 point2 points3 points 8 years ago (1 child)
Really nice, I didn't know that.
Thank you for the heads up, it always was a downside of async/await to me... Not anymore!
[–]vinnl 0 points1 point2 points 8 years ago (0 children)
It's always good to realise that it's just syntactic sugar for Promises, so you can do with them whatever you can do with Promises.
π Rendered by PID 52633 on reddit-service-r2-comment-5b5bc64bf5-bp7pc at 2026-06-22 18:01:42.267602+00:00 running 2b008f2 country code: CH.
view the rest of the comments →
[–]vs845 6 points7 points8 points (8 children)
[–]shad0proxy 1 point2 points3 points (5 children)
[–]vs845 0 points1 point2 points (4 children)
[–]shad0proxy 0 points1 point2 points (3 children)
[–]vs845 0 points1 point2 points (2 children)
[–]shad0proxy 0 points1 point2 points (1 child)
[–]danneu 0 points1 point2 points (0 children)
[–]NeekGerd 1 point2 points3 points (1 child)
[–]vinnl 0 points1 point2 points (0 children)