Introduction
At work, someone asked if there were any better ways to handle errors when using the async/await syntax in JavaScript. They didn’t like their beautiful, short, and readable lines of code suddenly wrapped with try/catches. I’ve also been frustrated with a variety of the enthusiasm online the past couple years around async/await only to be shown code examples that completely ignore error handling.
Below is an easier way to handle errors using async/await by returning what’s known in Functional Programming as an Either. Mine isn’t as formal as the FP community’s “left right”. It’s just simple JavaScript Object that follows the Node callback naming convention somewhat.
tl;dr; First option is to create Promises that only call success with an Either, and Promise.resolve in the catch with an Either, or second option is to use a simple wrapper function.
(more…)