All files / utils run.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 4/4
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                            23x 23x 23x 20x 12x   8x 4x 4x     3x        
import { isPromiseLike } from './isPromiseLike'
 
/**
 * 以数组的方式返回函数执行的结果,如果函数返回一个异步操作,将会执行该异步操作并将其结果作为函数执行的结果返回。
 *
 * @param fn 要执行的函数
 * @returns 返回 `[错误信息, 结果数据]`,错误信息为 `null` 表示没有错误
 * @example
 * ```typescript
 * // 函数执行成功
 * const [err, res] = await run(() => 'ok') // => [null, 'ok']
 * const [err, res] = await run(async () => 'ok') // => [null, 'ok']
 *
 * // 函数执行出错
 * const [err, res] = await run(() => { throw 'err' }) // => ['err']
 * const [err, res] = await run(async () => { throw 'err' }) // => ['err']
 * ```
 */
export function run<T extends () => any, R extends ReturnType<T>>(
  fn: T,
): Promise<
  [
    unknown,
    R extends PromiseLike<any>
      ? R extends PromiseLike<infer X>
        ? X
        : unknown
      : R,
  ]
> {
  return new Promise(resolve => {
    try {
      const res = fn()
      if (!isPromiseLike(res)) {
        return resolve([null, res])
      }
      return res.then(
        res => resolve([null, res]),
        err => resolve([err] as any),
      )
    } catch (err) {
      return resolve([err] as any)
    }
  })
}