Function: tryCatchAsync()
Call Signature
tryCatchAsync<
T>(fn):Promise<Result<T,unknown>>
Defined in: result/constructors/index.ts:109
Wraps an async function in a Result, catching any errors.
Type Parameters
T
T
Parameters
fn
() => Promise<T>
Async function that may throw
Returns
Promise<Result<T, unknown>>
Promise of Result containing either the value or error
Example
const fetchUser = async (id: string) =>
tryCatchAsync(async () => {
const res = await fetch(`/api/users/${id}`)
return res.json()
})
const result = await fetchUser('123')
// => Ok({ name: 'John' }) or Err(NetworkError)
Call Signature
tryCatchAsync<
T,E>(fn,mapError):Promise<Result<T,E>>
Defined in: result/constructors/index.ts:131
Wraps an async function in a Result with custom error mapping.
Type Parameters
T
T
E
E
Parameters
fn
() => Promise<T>
Async function that may throw
mapError
(error) => E
Function to transform the caught error
Returns
Promise<Result<T, E>>
Promise of Result containing either the value or mapped error
Example
const fetchUser = async (id: string) =>
tryCatchAsync(
async () => {
const res = await fetch(`/api/users/${id}`)
if (!res.ok) throw new Error(`HTTP ${res.status}`)
return res.json()
},
(e) => ({ code: 'FETCH_ERROR', message: String(e) })
)