2.1.2 • Published 5 years ago

attempt-x v2.1.2

Weekly downloads
12,916
License
MIT
Repository
github
Last release
5 years ago

attempt-x

Invokes function, returning an object of the results.

module.exports(fn, [...args])Object

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.

ParamTypeDescription
fnfunctionThe function to attempt.
...args*The arguments to invoke the function with.

Example

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];