try-invoke

1.0.0 • Public • Published

try-invoke

Attempts to invoke the given function, returning either the result or the caught error object.

MIT License

build:? coverage:?

Install

$ npm install --save try-invoke 

Usage

For more use-cases see the tests

 
var tryInvoke = require('try-invoke');
 
// tryInvoke(func, thisArg, args);
 
var obj = {
  add: function () {
    var result = 0;
    for (var i = 0, length = arguments.length; i < length; i++) {
      result += arguments[i]
    }
    return result;
  }
}
    
tryInvoke(function (a, b, c, d) {
  return this.add(a, b, c, d);
}, obj, [1, 2, 3, 4]);
 
// => 10

Related

  • invoke-fn - A faster alternative to Function#apply.
  • invoke-before - Returns a function that will only be executed before being called N times.
  • invoke-after - Returns a function that will only be executed after being called N times.
  • invoke-once - Restrict the given function only be invoked once.
  • is-native - Checks if the given value is a native function.
  • to-source-code - Converts function to its source code.

Contributing

Pull requests and stars are highly welcome.

For bugs and feature requests, please create an issue.

Package Sidebar

Install

npm i try-invoke

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • bubkoo