ts-data-forge
Version:
[](https://www.npmjs.com/package/ts-data-forge) [](https://www.npmjs.com/package/ts-data-forge) [ • 1.09 kB
JavaScript
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