to.ts 515 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<DATA = any, ERROR = any>(
  promise: any,
  errorExt?: string
): Promise<[ERROR, DATA]> {
  return promise
    .then((data) => [null, data])
    .catch((err) => {
      if (errorExt) {
        const parsedError = Object.assign({}, err, errorExt);
        return [parsedError, undefined];
      }
      return [err, undefined];
    });
}