[fabric/core] Refactor AsyncResult to accept MaybePromise in tryFrom and from functions

This commit is contained in:
Pablo Baleztena 2024-10-14 09:46:02 -03:00
parent 559a3f3c22
commit 6b46677be9

View File

@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import { TaggedError } from "../error/tagged-error.js"; import { TaggedError } from "../error/tagged-error.js";
import { UnexpectedError } from "../error/unexpected-error.js"; import { UnexpectedError } from "../error/unexpected-error.js";
import { MaybePromise } from "../types/maybe-promise.js";
import { Result } from "./result.js"; import { Result } from "./result.js";
/** /**
@ -14,7 +15,7 @@ export type AsyncResult<
export namespace AsyncResult { export namespace AsyncResult {
export async function tryFrom<T, TError extends TaggedError>( export async function tryFrom<T, TError extends TaggedError>(
fn: () => Promise<T>, fn: () => MaybePromise<T>,
errorMapper: (error: any) => TError, errorMapper: (error: any) => TError,
): AsyncResult<T, TError> { ): AsyncResult<T, TError> {
try { try {
@ -25,8 +26,8 @@ export namespace AsyncResult {
} }
export async function from<T>( export async function from<T>(
fn: () => Promise<T>, fn: () => MaybePromise<T>,
): AsyncResult<T, UnexpectedError> { ): AsyncResult<T, never> {
return tryFrom(fn, (error) => new UnexpectedError(error)); return tryFrom(fn, (error) => new UnexpectedError(error) as never);
} }
} }