Convert a Promise<T> into a ResultAsync. Rejection is captured and passed through onError — the error type is entirely user-controlled, so pick a domain-specific shape rather than leaking unknown.
Promise<T>
onError
unknown
const fetchUser = (id: string) => fromPromise( fetch(`/users/${id}`).then(r => r.json() as Promise<User>), (cause) => ({ code: "FETCH_FAILED", cause }), ) Copy
const fetchUser = (id: string) => fromPromise( fetch(`/users/${id}`).then(r => r.json() as Promise<User>), (cause) => ({ code: "FETCH_FAILED", cause }), )
Convert a
Promise<T>into a ResultAsync. Rejection is captured and passed throughonError— the error type is entirely user-controlled, so pick a domain-specific shape rather than leakingunknown.