to.ts
534 Bytes
/**
* @description use to function capture await throw error
* @param promise
* @param errorExt - Additional Information you can pass to the err object
*/
export function to<T, E = any>(
promise: Promise<T>,
errorExt?: string
): Promise<[Nullable<E>, T | undefined]> {
return promise
.then((data) => [null, data] as [null, T])
.catch((err) => {
if (errorExt) {
const parsedError = Object.assign({}, err, errorExt)
return [parsedError, undefined]
}
return [err, undefined]
});
}