toResult
Converts a PromiseLike to a Promise<Result>
Usage
Converts a PromiseLike to a Promise<Result>.
import { function toResult<T>(promise: PromiseLike<T>): Promise<Result<T>>
Converts a PromiseLike to a Promise<Result>.
Note: If the given promise throws a non-Error value, it will be
rethrown.
toResult, type Result<TResult, TError extends Error = Error> = Ok<TResult> | Err<TError>
A result tuple.
First index is the error, second index is the result.
Result } from 'radashi'
const const good: () => Promise<number>
good = async (): interface Promise<T>
Represents the completion of an asynchronous operation
Promise<number> => 1const const bad: () => Promise<number>
bad = async (): interface Promise<T>
Represents the completion of an asynchronous operation
Promise<number> => { throw new var Error: ErrorConstructornew (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error('bad')}
const const goodResult: Result<number>
goodResult = await toResult<number>(promise: PromiseLike<number>): Promise<Result<number>>
Converts a PromiseLike to a Promise<Result>.
Note: If the given promise throws a non-Error value, it will be
rethrown.
toResult(const good: () => Promise<number>
good())// => [undefined, 1]
const const badResult: Result<number>
badResult = await toResult<number>(promise: PromiseLike<number>): Promise<Result<number>>
Converts a PromiseLike to a Promise<Result>.
Note: If the given promise throws a non-Error value, it will be
rethrown.
toResult(const bad: () => Promise<number>
bad())// => [Error('bad'), undefined]Throwing non-Error values
If the given promise throws a non-Error value, it will be rethrown. This ensures the Result always contains an Error value or undefined.
import { function toResult<T>(promise: PromiseLike<T>): Promise<Result<T>>
Converts a PromiseLike to a Promise<Result>.
Note: If the given promise throws a non-Error value, it will be
rethrown.
toResult } from 'radashi'
// This won't catch the rejected promise, because it was// rejected with a string, not an Error.const const res: Result<never>
res = await toResult<never>(promise: PromiseLike<never>): Promise<Result<never>>
Converts a PromiseLike to a Promise<Result>.
Note: If the given promise throws a non-Error value, it will be
rethrown.
toResult(var Promise: PromiseConstructor
Represents the completion of an asynchronous operation
Promise.PromiseConstructor.reject<never>(reason?: any): Promise<never>
Creates a new rejected promise for the provided reason.
reject('test error'))Synchronous errors
This function won’t catch synchronous errors. Only rejected promises are converted to an Err.
import { function toResult<T>(promise: PromiseLike<T>): Promise<Result<T>>
Converts a PromiseLike to a Promise<Result>.
Note: If the given promise throws a non-Error value, it will be
rethrown.
toResult } from 'radashi'
function function bad(): Promise<unknown>
bad() { if (var Math: Math
An intrinsic object that provides basic mathematics functionality and constants.
Math.Math.random(): number
Returns a pseudorandom number between 0 and 1.
random() > 0.5) { // ⚠️ This error won't be caught by `toResult`. To fix this, add // the `async` keyword to the containing function. throw new var Error: ErrorConstructornew (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error('bad') } return new var Promise: PromiseConstructornew <unknown>(executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void) => Promise<unknown>
Creates a new Promise.
Promise((resolve: (value: unknown) => void
resolve, reject: (reason?: any) => void
reject) => { /* ... */ })}
const res = await toResult<unknown>(promise: PromiseLike<unknown>): Promise<Result<unknown>>
Converts a PromiseLike to a Promise<Result>.
Note: If the given promise throws a non-Error value, it will be
rethrown.
toResult(function bad(): Promise<unknown>
bad())const res: Result<unknown>