attempt-x

Invokes function, returning an object of the results.

MIT 32 个版本
安装
npm install attempt-x
yarn add attempt-x
pnpm add attempt-x
bun add attempt-x
README

Travis status Dependency status devDependency status npm version jsDelivr hits bettercodehub score Coverage Status

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.

Param Type Description
fn function The 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];
版本列表
2.1.2 2019-08-28
2.1.1 2019-08-20
2.1.0 2019-08-14
2.0.23 2019-07-31
2.0.22 2019-07-27
2.0.21 2019-07-27
2.0.20 2019-07-26
2.0.19 2019-07-25
2.0.18 2019-07-24
2.0.17 2019-07-24
2.0.16 2019-07-23
2.0.15 2019-07-22
2.0.14 2019-07-21
2.0.13 2019-07-21
2.0.12 2019-07-19
2.0.11 2019-07-18
2.0.10 2019-07-18
2.0.9 2019-07-17
2.0.8 2019-07-17
2.0.7 2019-07-17
2.0.6 2019-07-16
2.0.5 2019-07-16
2.0.4 2019-07-15
2.0.3 2019-07-14
2.0.2 2019-07-13
2.0.1 2019-07-12
2.0.0 2019-07-10
1.1.3 2018-02-24
1.1.2 2018-02-24
1.1.1 2017-09-25
1.1.0 2017-09-05
1.0.0 2017-09-03