UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

36 lines (33 loc) 1.09 kB
import { err } from './result-err.mjs'; import { ok } from './result-ok.mjs'; /** * Converts a Promise into a Promise that resolves to a `Result`. If the input * Promise resolves, the `Result` will be `Ok` with the resolved value. If the * input Promise rejects, the `Result` will be `Err` with the rejection * reason. * * @example * * ```ts * const successPromise = Result.fromPromise(Promise.resolve('ok')); * * const failurePromise = Result.fromPromise(Promise.reject(new Error('fail'))); * * const resolved = await successPromise; * * const rejected = await failurePromise; * * assert.deepStrictEqual(resolved, Result.ok('ok')); * * assert.isTrue(Result.isErr(rejected)); * ``` * * @template P The type of the input Promise. * @param promise The Promise to convert. * @returns A Promise that resolves to `Result<UnwrapPromise<P>, unknown>`. */ const fromPromise = (promise) => // eslint-disable-next-line total-functions/no-unsafe-type-assertion promise.then((v) => ok(v)).catch(err); export { fromPromise }; //# sourceMappingURL=result-from-promise.mjs.map