Invokes function, returning an object of the results.
npm install attempt-x href="https://travis-ci.org/Xotic750/attempt-x"
title="Travis status"> src="https://travis-ci.org/Xotic750/attempt-x.svg?branch=master"
alt="Travis status" height="18">
href="https://david-dm.org/Xotic750/attempt-x"
title="Dependency status"> alt="Dependency status" height="18"/>
href="https://david-dm.org/Xotic750/attempt-x?type=dev"
title="devDependency status"> alt="devDependency status" height="18"/>
href="https://badge.fury.io/js/attempt-x"
title="npm version"> alt="npm version" height="18">
href="https://www.jsdelivr.com/package/npm/attempt-x"
title="jsDelivr hits"> alt="jsDelivr hits" height="18">
href="https://bettercodehub.com/results/Xotic750/attempt-x"
title="bettercodehub score"> alt="bettercodehub score" height="18">
href="https://coveralls.io/github/Xotic750/attempt-x?branch=master"
title="Coverage Status"> alt="Coverage Status" height="18">
Invokes function, returning an object of the results.
This method attempts to invoke the function, returning either the result or
the caught error object. Any additional arguments are provided to the
function when it's invoked.
Kind: Exported function
Returns: Object - Returns an object of the result.
| Param | Type | Description |
| --------- | --------------------- | ------------------------------------------ |
| fn | function | The function to attempt. |
| [...args] | \* | The arguments to invoke the function with. |
Example
``js
import attempt from 'attempt-x';
function thrower() {
throw new Error('Threw');
}
attempt(thrower, 1, 2);
// {
// threw: true,
// value: // Error('Threw') object
// }
function sumArgs(a, b) {
return a + b;
}
attempt(sumArgs, 1, 2);
// {
// threw: false,
// value: 3
// }
const thisArg = [];
function pusher(a, b) {
return this.push(a, b);
}
attempt.call(thisArg, pusher, 1, 2);
// {
// threw: false,
// value: 2
// }
// thisArg => [1, 2];
``