Overview
Converts an async function that might throw into a function that returns aResultAsync. This is the safest way to handle third-party async functions.
Signature
(...args: A) => Promise<R>
required
An async function that might throw or return a rejecting Promise
(err: unknown) => E
Optional function to map thrown errors to a known type. Defaults to returning the error as-is.
ResultAsync<R, E>
Usage
Basic usage
File operations
API calls
Database operations
Why use fromThrowable over fromPromise?
fromThrowable is safer because it catches both synchronous throws and Promise rejections.Key characteristics
- Catches all errors: Both sync throws and Promise rejections
- Preserves parameters: Wrapped function has same signature
- Type safety: Error type is controlled by errorFn
- Reusable: Create the wrapper once, use many times
Comparison table
Top-level export
Also available as a standalone function:Related
Result.fromThrowable
Synchronous version
from-promise
Alternative for Promise objects