Skip to content

Function: tryRun()

ts
function tryRun<T>(fn): Promise<T | null>;

Safely executes a function (sync or async) and returns its result, or null if an error occurs. 安全地执行函数(同步或异步)并返回其结果,如果发生错误则返回 null。

Type Parameters

Type ParameterDescription
TThe return type of the function / 函数的返回类型

Parameters

ParameterTypeDescription
fn() => T | Promise<T>The function to execute safely / 要安全执行的函数

Returns

Promise<T | null>

A Promise that resolves to the function result or null if an error occurs / 返回一个 Promise,解析为函数结果或在发生错误时返回 null

Example

typescript
// Synchronous function success case / 同步函数成功情况
const result = await tryRun(() => JSON.parse('{"name": "test"}'))
result // => { name: "test" }

// Synchronous function error case / 同步函数错误情况
const errorResult = await tryRun(() => JSON.parse('invalid json'))
errorResult // => null

// Asynchronous function success case / 异步函数成功情况
const asyncResult = await tryRun(async () => {
  await new Promise(resolve => setTimeout(resolve, 100))
  return { data: 'async test' }
})
asyncResult // => { data: 'async test' }

// Asynchronous function error case / 异步函数错误情况
const asyncError = await tryRun(async () => {
  throw new Error('async error')
})
asyncError // => null

// With calculation / 计算示例
const calc = await tryRun(() => 10 / 2)
calc // => 5

Since

1.2.0

Released under the MIT License.