Package Exports
- evl
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (evl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
EVL
😈 Function fallback when error.
Likes NVL but for Error fallback.
Installation
Via NPM:
npm install evl
Via Yarn:
yarn add evl
Usage
const evl = require('evl');
let err = () => {
throw new Error('An error. 😱');
};
let one = () => 1;
let two = () => 2;
let add = (a, b) => a + b;
let multiply = (a, b) => a * b;
/*
* Simple usage
*/
let a = evl(one, two); // a is 1
let b = evl(err, two); // b is 2
/*
* With same arguments
*/
// To call add(1, 2) or multiply(1, 2)
let c = evl(add, multiply, false)(1, 2); // c is 3 (1 + 2 from add function)
// To call err(1, 2) or multiply(1, 2)
let d = evl(err, multiply, false)(1, 2); // d is 2 (1 * 2 from multiply function)
/*
* With different arguments
*/
// To call add(1, 2) or multiply(3, 4)
let e = evl(add, multiply, false)([1, 2], [3, 4]); // e is 3 (1 + 2 from add function)
// To call err(1, 2) or multiply(3, 4)
let f = evl(err, multiply, false)([1, 2], [3, 4]); // f is 12 (3 * 4 from multiply function)
You can also pass non-function as argument.
const evl = require('evl');
let err = () => {
throw new Error('An error. 💩');
};
evl(err, 'I am fallback value.') // -> 'I am fallback value.'
API
evl(mainFunction, fallbackFunction, [noArguments])
mainFunction
Type: Function
A main function that you expect it to work.
If you pass non-function value to this parameter,
evl
will return it back. Because it can't be called. So it can't throw an error.
fallbackFunction
Type: Function
A fallback function that will work when main function throw an error.
If you pass non-function value to this parameter,
evl
will return that value back when main function not work.
noArguments
Type: Boolean
Default: true
If true
, evl
will call the main function or fallback function and return the value back instantly.
If false
, evl
will return the invoke
function.
Invoke Function
invoke([args...])
Return:
Any
Call main function or fallback function with all
arguments
frominvoke
function and return the value back.invoke(mainFuncArgs, fallbackFuncArgs)
Return:
Any
Call main function or fallback function with its own arguments and return the value back.
mainFuncArgs
Type:
Array
The arguments of main function.
fallbackFuncArgs
Type:
Array
The arguments of fallback function