代码
export function toCatch2<T> (fn: Promise<T>): Promise<[Error | null, T]> {
// @ts-ignore
return fn.then((res: T) => [null, res]).catch((err: Error) => [err, null])
}
使用
传入一个promise,可以获取到异步结果和捕获的错误
const [err, res1] = await toCatch2(requestPaymentPromise)