UNPKG

typescript-monads

Version:
34 lines 1.3 kB
import { IMaybe } from '../maybe.interface'; /** * Wraps a function that returns a Promise to catch errors and return a Maybe. * * Executes the provided function and converts the result: * - If the function's Promise resolves, returns a Some containing the result * - If the function's Promise rejects, returns None * * This is particularly useful for handling API calls or other operations that may fail, * allowing for more functional error handling without explicit try/catch blocks. * * @param fn The function that returns a Promise * @returns A Promise that resolves to a Maybe containing the result if successful * * @example * // Without using tryPromiseToMaybe * function fetchUserData(userId) { * return api.fetchUserData(userId) * .then(data => maybe(data)) * .catch(() => none()); * } * * // Using tryPromiseToMaybe * tryPromiseToMaybe(() => api.fetchUserData(userId)) * .then(userDataMaybe => { * // Then use the Maybe as normal * userDataMaybe.match({ * some: data => displayUserData(data), * none: () => showErrorMessage('Could not load user data') * }); * }); */ export declare function tryPromiseToMaybe<T>(fn: () => Promise<T>): Promise<IMaybe<NonNullable<T>>>; //# sourceMappingURL=try-promise-to-maybe.d.ts.map