ResultTools is a standard library for JavaScript functional programming.
The ResultTools library goal is to provide developers an auxiliary tool for modeling results of sync and async operations. Annuls the try/catch need, therefor, allowing better control of sequence operations, error handling and propagation.
Async operations can be performed by the _try
and trySync
operation to perform sync operations.
The operation results are, Ok(success Value)
or Error(error Value)
and can be accessed through the get
or orElse
operations. These results offer tests through the isOk
or isError
operations. Based on the operation results it is possible to use fluent features, such as .chain
, .chainSync
, .serial
, .serialSync
, .map
and .mapSync
to execute sync and async sequence operations in a controlled way.
Also, there is no need to perform the try operation, since it can be used in isolation.
ResultTools can be installed through npm:
$ npm install result-tools
-
_try(fn)
Used to execute async operations. The operation result can be either Ok(success Value) or Error(error Value).const Result = require('result-tool'); const fnPromise = () => Promise.reject(-1); Result._try(fnPromise); // ==> Result.Error(-1)
const Result = require('result-tool'); const fnPromise = () => Promise.resolve('Annuls the try/catch need'); Result._try(fnPromise); // ==> Result.Ok('Annuls the try/catch need')
-
trySync(fn)
Used to execute sync operations. The operation result can be either Ok(success Value) or Error(error Value).const Result = require('result-tool'); const fnPromise = () => Error(-1); Result.trySync(fnPromise); // ==> Result.Error(-1)
const Result = require('result-tool'); const fnPromise = () => Ok('Annuls the try/catch need'); Result.trySync(fnPromise); // ==> Result.Ok('Annuls the try/catch need')
-
chain(fn)
Used to execute async operations where the arguments are the results from the last operation, generating a new result.const Result = require('result-tool'); const fnPromise = () => Promise.reject(-1); //operation will not be performed const fnChain = (arg) => Promise.resolve(arg); Result ._try(fnPromise) .chain(fnChain) .run(); // ==> Result.Error(-1)
const Result = require('result-tool'); const fnPromise = () => Promise.resolve('Annuls the'); const fnChain = (arg) => Promise.resolve(`${arg} try/catch need`); Result ._try(fnPromise) .chain(fnChain) .run(); // ==> Result.Ok('Annuls the try/catch need')
-
chainSync(fn)
Used to execute sync operations where the arguments are the results from the last operation, generating a new result.const Result = require('result-tool'); const fnPromise = () => Error(-1); //operation will not be performed const fnChain = (arg) => Ok(arg); Result .trySync(fnPromise) .chainSync(fnChain) .run(); // ==> Result.Error(-1)
const Result = require('result-tool'); const fnPromise = () => Ok('Annuls the'); const fnChain = (arg) => Ok(`${arg} try/catch need`); Result .trySync(fnPromise) .chainSync(fnChain) .run(); // ==> Result.Ok('Annuls the try/catch need')
-
serial(fn, fnArgs)
Used to execute async operations where the arguments could be received, generating a new result.const Result = require('result-tool'); const fnPromise = () => Promise.reject(-1); //operation will not be performed const fnSerial = (arg) => Promise.resolve(arg); Result ._try(fnPromise) .serial(fnSerial, 2) .run(); // ==> Result.Error(-1)
const Result = require('result-tool'); const fnPromise = () => Promise.resolve(4); const fnSerial = (arg) => Promise.resolve(arg); Result ._try(fnPromise) .serial(fnSerial, 2) .run(); // ==> Result.Ok([4, 2])
-
serialSync(fn, fnArgs)
Used to execute sync operations where the arguments could be received, generating a new result.const Result = require('result-tool'); const fnPromise = () => Error(-1); //operation will not be performed const fnSerial = (arg) => Ok(arg); Result .trySync(fnPromise) .serialSync(fnSerial, 2) .run(); // ==> Result.Error(-1)
const Result = require('result-tool'); const fnPromise = () => Ok(4); const fnSerial = (arg) => Ok(arg); Result .trySync(fnPromise) .serialSync(fnSerial, 2) .run(); // ==> Result.Ok([4, 2])
-
map(fn)
Used to execute async transformation operations where the arguments are the results from the last operation, generating a new result.const Result = require('result-tool'); const fnPromise = () => Promise.reject(-1); //operation will not be performed const fnMap = (arg) => Promise.resolve(arg); Result ._try(fnPromise) .map(fnMap) .run(); // ==> Result.Error(-1)
const Result = require('result-tool'); const fnPromise = () => Promise.resolve('Annuls the'); const fnMap = (arg) => Promise.resolve(`${arg} try/catch need`); Result ._try(fnPromise) .map(fnMap) .run(); // ==> Result.Ok('Annuls the try/catch need')
-
mapSync(fn)
Used to execute sync transformation operations where the arguments are the results from the last operation, generating a new result.const Result = require('result-tool'); const fnPromise = () => Error(-1); //operation will not be performed const fnMap = (arg) => Ok(arg); Result .trySync(fnPromise) .mapSync(fnMap) .run(); // ==> Result.Error(-1)
const Result = require('result-tool'); const fnPromise = () => Ok('Annuls the'); const fnMap = (arg) => Ok(`${arg} try/catch need`); Result .trySync(fnPromise) .mapSync(fnMap) .run(); // ==> Result.Ok('Annuls the try/catch need')
-
get
Used to access the result value.const Result = require('result-tool'); const fnPromise = () => Error(-1); Result .trySync(fnPromise) .get(); // ==> -1
const Result = require('result-tool'); const fnPromise = () => Ok('Annuls the try/catch need'); Result .trySync(fnPromise) .get(); // ==> 'Annuls the try/catch need'
-
orElse(arg)
Used to access the success value or argument value in error cases.const Result = require('result-tool'); const fnPromise = () => Error(-1); Result .trySync(fnPromise) .orElse('There is an error'); // ==> There is an error
const Result = require('result-tool'); const fnPromise = () => Ok('Annuls the try/catch need'); Result .trySync(fnPromise) .orElse('There is an error'); // ==> 'Annuls the try/catch need'
-
isOk
Used to test the result value. Returns True if the result value is Ok.const Result = require('result-tool'); const fnPromise = () => Error(-1); Result .trySync(fnPromise) .isOk(); // ==> false
const Result = require('result-tool'); const fnPromise = () => Ok('Annuls the try/catch need'); Result .trySync(fnPromise) .isOk(); // ==> true
-
isError
Used to test the result value. Returns True if the result value is Error.
const Result = require('result-tool');
const fnPromise = () => Error(-1);
Result
.trySync(fnPromise)
.isError();
// ==> true
const Result = require('result-tool');
const fnPromise = () => Ok('Annuls the try/catch need');
Result
.trySync(fnPromise)
.isError();
// ==> false
Result Tools is written for the ECMAScript 2015 platform. If you're running your program in an older platform, you'll need es5-shim and es6-shim.
See the LICENSE
file in this repository for detailed information.