Attempt to execute a function and return the result or error. Returns a tuple where:
Only applicable to synchronous execution functions
尝试执行函数并返回结果或错误。返回一个元组,其中:
仅适用于同步执行函数
The function to execute - 要执行的函数
The arguments to pass to the function - 要传递给函数的参数
const [error, result] = attempt(() => 12) // [null, 12]const [error, result] = attempt(() => { throw new Error('error') }) // [Error, null] Copy
const [error, result] = attempt(() => 12) // [null, 12]const [error, result] = attempt(() => { throw new Error('error') }) // [Error, null]
const add = (a, b) => a + bconst [error, result] = attempt(add, 1, 2) // [null, 3]// orconst [error, result] = attempt(() => add(1, 2)) // [null, 3] Copy
const add = (a, b) => a + bconst [error, result] = attempt(add, 1, 2) // [null, 3]// orconst [error, result] = attempt(() => add(1, 2)) // [null, 3]
Attempt to execute a function and return the result or error. Returns a tuple where:
Only applicable to synchronous execution functions
尝试执行函数并返回结果或错误。返回一个元组,其中:
仅适用于同步执行函数