January 2020
Intermediate to advanced
470 pages
11h 13m
English
If we are calling functions that may throw exceptions and we want to do so in a functional way, we could use the Try monad to encapsulate the function result or the exception. The idea is basically the same as the Either monad: the only difference is in the constructor, which receives a function and calls it:
This can be seen in the following code:
class Try extends Either { constructor(fn, msg) { try { return Either.of(null, fn()); } catch (e) { return Either.of(msg || e, null); } } static of(fn, msg) { return new Try(fn, msg); }}
Now, we can invoke any function, ...
Read now
Unlock full access