ts-fortress
    Preparing search index...

    Variable fromPromiseConst

    fromPromise: <P extends Promise<unknown>>(
        promise: P,
    ) => Promise<Result<UnwrapPromise<P>, unknown>>

    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. A rejection with a PanicError is rethrown instead: a bug is not a recoverable failure.

    The error channel is unknown, since the rejection reason is carried through untouched. For a concrete error type, use AsyncResult.fromPromise with a mapError function; called without one it behaves exactly as this function does.

    Type Declaration

      • <P extends Promise<unknown>>(
            promise: P,
        ): Promise<Result<UnwrapPromise<P>, unknown>>
      • Type Parameters

        • P extends Promise<unknown>

          The type of the input Promise.

        Parameters

        • promise: P

          The Promise to convert.

        Returns Promise<Result<UnwrapPromise<P>, unknown>>

        A Promise that resolves to Result<UnwrapPromise<P>, unknown>.

    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));