3 ms·
> async stuff silently swallows exceptions unless I put try{} catch(err) { console.trace(err) } everywhere Huh? That's not how async errors work in Node. Try/C
by rhinoceraptor 11y ago
> async stuff silently swallows exceptions unless I put try{} catch(err) { console.trace(err) } everywhere
Huh? That's not how async errors work in Node. Try/Catch is not async, the catch block will not magically transfer to your callback function.
You check for the error as the first parameter in your callback, that's the standard way of error handling. Throwing errors in Node is considered by most to be an anti-pattern.
- narrator 11y agoThat's nice in theory, but third party libraries may throw exceptions if unexpected things happen at runtime. You'd still have to put the try catch block there and call the callback in the catch block everywhere.
- rhinoceraptor 11y agoThat's only for non-async stuff, which is very rare. If you're using some wacky 3rd party code which throws an Error instead of the accepted convention of (err, response) callback arguments (or returning a promise), I suggest not using it.