3 ms·
IIFEs are an option: const result = await (() => { try { return funcThatReturnSomeType(); } catch (err) { doSomethingWithErr(er
by yawaramin 7y ago
IIFEs are an option:
const result = await (() => {
try {
return funcThatReturnSomeType();
} catch (err) {
doSomethingWithErr(err);
}
})();
- smt88 7y agoThat is so much harder to read though...
- saurik 7y agoI don't think this is correct, as the try catch will only catch errors that happen while returning the promise, not awaiting it; you need to do this: const result = await (async () => { try { return await funcThatReturnSomeType(); } catch (err) { doSomethingWithErr(err); } })();
- yawaramin 7y agoYou may be right, proving the old adage that the best way to get an answer on the internet is to post a wrong answer ;-)
- jkoudys 7y agoIffys are a nice go-to in ts in general, because type inference is so handy and I'd rather not have a big pile of typed `let`s at the top that I have to assign into later. Even something as simple as a switch plays nicer if you iffy it and use it like an expression (similar to a match is rust)