API reference › @evolu/common › Result › tryAsync
function tryAsync<T, E>(
lazyPromise: Lazy<Promise<T>>,
mapError: (error: unknown) => E,
): Promise<Result<T, E>>;
Defined in: packages/common/src/Result.ts:440
Wraps an async function that may throw, returning a Result.
Use this when the rejection should become a typed, recoverable error for the caller. Do not use it for failures that should terminate the current flow and propagate to a global handler.
Example
const fetchJson = (url: string): Promise<Result<unknown, FetchError>> =>
tryAsync(
async () => {
const response = await fetch(url);
if (!response.ok) throw new Error(`Status ${response.status}`);
return response.json();
},
(error) => ({ type: "FetchError", message: String(error) }),
);