Variable: fromPromise()
readonlyfromPromise: {<T>(executor):AsyncResult<T,Error>; <T,E>(executor,onError):AsyncResult<T,E>; }
Defined in: core/factories.ts:196
Call Signature
<
T>(executor):AsyncResult<T,Error>
Wraps async function execution in Result, capturing rejections.
Converts Promises that may reject into Results, allowing explicit handling without try/catch or .catch().
Type Parameters
T
T
Resolved value type
Parameters
executor
() => Promise<T>
Async function to execute
Returns
AsyncResult<T, Error>
Promise of Ok with value or Err if rejects
Examples
// Fetch API
const response = await Result.fromPromise(
async () => {
const res = await fetch('/api/user')
return res.json()
}
)
if (response.isOk()) {
console.log(response.unwrap())
}// Chained async operations
const user = await Result.fromPromise(async () => {
const data = await db.query('SELECT * FROM users WHERE id = ?', [id])
if (!data[0]) throw new Error('User not found')
return data[0]
})// Async file I/O
const content = await Result.fromPromise(
() => fs.promises.readFile('file.txt', 'utf-8')
)Call Signature
<
T,E>(executor,onError):AsyncResult<T,E>
Wraps Promise in Result with custom error transformation.
Type Parameters
T
T
Resolved value type
E
E
Error type
Parameters
executor
() => Promise<T>
Async function to execute
onError
(error) => E
Function that transforms the rejection error
Returns
AsyncResult<T, E>
Promise of Ok or Err with custom error
Examples
// Typed error
type NetworkError = { type: 'network'; status?: number }
const data = await Result.fromPromise(
() => fetch('/api/data').then(r => r.json()),
(err): NetworkError => ({
type: 'network',
status: err instanceof Response ? err.status : undefined
})
)// Contextualizing errors
const user = await Result.fromPromise(
() => fetchUser(id),
(err) => new Error(`Failed to fetch user ${id}: ${err}`)
)