Elegant Error Handling with the JavaScript “Either” Monad

Elegant Error Handling with the JavaScript “Either” Monad

If we end up with an error, it would also be nice to have a way to print that too:

And once we have all of those in place, we can put them together to create our function that will process each row. Perhaps we could use something like this to handle our errors…

Getting back to our problem, we want to define a happy path and a sad path for our code. Let’s create a method that will take a function and run it if we’re on the happy path, but ignore it if we’re on the sad path:

Then we could do something like this:

We’re getting closer to something useful, but we’re not quite there yet.

Source: jrsinclair.com