fromPromise
@ts-rust/std / Result / fromPromise
Function: fromPromise()
function fromPromise<T, E>(promise, mkErr): PendingResult<T, E>;
Defined in: packages/std/src/result/result.ts:526
Wraps a promise in a PendingResult, mapping rejection through mkErr.
This is the promise-taking counterpart of runAsync: pass an already
created promise instead of a factory. If the promise fulfills, the pending
result resolves to Ok. If it rejects, the reason is passed to mkErr
and the pending result resolves to Err.
Type Parameters
| Type Parameter |
|---|
|
|
Parameters
| Parameter | Type | Description |
|---|---|---|
|
| A promise of the successful value. |
| ( | A function that converts a rejection reason into an error of type |
Returns
PendingResult<T, E>
A PendingResult that resolves to Ok or Err.
Example
const pendingRes = fromPromise(
fetch("https://api.example.com/text").then((res) => res.text()),
(e) => new Error(`Fetch failed: ${JSON.stringify(e)}`),
);
const res = await pendingRes;
if (res.isErr()) {
console.log(res.unwrapErr().expected?.message);
}