attempt source npm

_.attempt(func)

尝试调用函数,返回结果 或者 错误对象。 任何附加的参数都会在调用时传给函数。

参数

  1. func (Function)

    要调用的函数

返回值 (*)

返回函数结果或者错误对象

示例

// 避免因为错误的选择器而抛出
var elements = _.attempt(function(selector) {
  return document.querySelectorAll(selector);
}, '>_>');

if (_.isError(elements)) {
  elements = [];
}