阅读(3973) (0)

Lodash _.attempt

2021-09-23 14:42:39 更新

_.attempt(func, [args])

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

添加版本

3.0.0

参数

  1. func (Function): 要尝试调用的函数。
  2. [args] (...*): 调用func时,传递的参数。

返回

(*): 返回func结果或者错误对象。

例子

// Avoid throwing errors for invalid selectors.
var elements = _.attempt(function(selector) {  return document.querySelectorAll(selector);}, '>_>');
 if (_.isError(elements)) {  elements = [];}