ResultTools
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.
Installation
ResultTools can be installed through npm:
$ npm install result-tools
Example
-
_try(fn)
Used to execute async operations. The operation result can be either Ok(success Value) or Error(error Value).###### error case:
const Result = ;const fnPromise = Promise;Result;// ==> Result.Error(-1)###### success case:
const Result = ;const fnPromise = Promise;Result;// ==> 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).###### error case:
const Result = ;const fnPromise = Error-1;Result;// ==> Result.Error(-1)###### success case:
const Result = ;const fnPromise = ;Result;// ==> 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.###### error case:
const Result = ;const fnPromise = Promise;//operation will not be performedconst fnChain = Promise;Result;// ==> Result.Error(-1)###### success case:
const Result = ;const fnPromise = Promise;const fnChain = Promise;Result;// ==> 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.###### error case:
const Result = ;const fnPromise = Error-1;//operation will not be performedconst fnChain = ;Result;// ==> Result.Error(-1)###### success case:
const Result = ;const fnPromise = ;const fnChain = ;Result;// ==> 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.###### error case:
const Result = ;const fnPromise = Promise;//operation will not be performedconst fnSerial = Promise;Result;// ==> Result.Error(-1)###### success case:
const Result = ;const fnPromise = Promise;const fnSerial = Promise;Result;// ==> Result.Ok([4, 2]) -
serialSync(fn, fnArgs)
Used to execute sync operations where the arguments could be received, generating a new result.###### error case:
const Result = ;const fnPromise = Error-1;//operation will not be performedconst fnSerial = ;Result;// ==> Result.Error(-1)###### success case:
const Result = ;const fnPromise = ;const fnSerial = ;Result;// ==> 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.###### error case:
const Result = ;const fnPromise = Promise;//operation will not be performedconst fnMap = Promise;Result;// ==> Result.Error(-1)###### success case:
const Result = ;const fnPromise = Promise;const fnMap = Promise;Result;// ==> 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.###### error case:
const Result = ;const fnPromise = Error-1;//operation will not be performedconst fnMap = ;Result;// ==> Result.Error(-1)###### success case:
const Result = ;const fnPromise = ;const fnMap = ;Result;// ==> Result.Ok('Annuls the try/catch need') -
get
Used to access the result value.###### error case:
const Result = ;const fnPromise = Error-1;Result;// ==> -1###### success case:
const Result = ;const fnPromise = ;Result;// ==> 'Annuls the try/catch need' -
orElse(arg)
Used to access the success value or argument value in error cases.###### error case:
const Result = ;const fnPromise = Error-1;Result;// ==> There is an error###### success case:
const Result = ;const fnPromise = ;Result;// ==> 'Annuls the try/catch need' -
isOk
Used to test the result value. Returns True if the result value is Ok.###### error case:
const Result = ;const fnPromise = Error-1;Result;// ==> false###### success case:
const Result = ;const fnPromise = ;Result;// ==> true -
isError
Used to test the result value. Returns True if the result value is Error.
error case:
const Result = ; const fnPromise = Error-1; Result ; // ==> true
###### success case:
const Result = ; const fnPromise = ; Result ; // ==> false
Supported platforms
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.
Having trouble using ResultTools?
ResultTools discussion channels and information
License
See the LICENSE
file in this repository for detailed information.