Skip to content

toResult

Converts a PromiseLike to a Promise<Result>

217 bytes
currently in beta

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.

@seehttps://radashi.js.org/reference/async/toResult

@example

import { toResult, Result } from 'radashi'
const good = async (): Promise<number> => 1
const bad = async (): Promise<number> => { throw new Error('bad') }
const goodResult = await toResult(good())
// => [undefined, 1]
const badResult = await toResult(bad())
// => [Error('bad'), undefined]

@version12.4.0

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.

@example

type MyResult = Result<string>
// ^? Ok<string> | Err<Error>
type MyResult2 = Result<string, TypeError>
// ^? Ok<string> | Err<TypeError>

Result
} from 'radashi'
const
const good: () => Promise<number>
good
= async ():
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<number> => 1
const
const bad: () => Promise<number>
bad
= async ():
interface Promise<T>

Represents the completion of an asynchronous operation

Promise
<number> => {
throw new
var Error: ErrorConstructor
new (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.

@seehttps://radashi.js.org/reference/async/toResult

@example

import { toResult, Result } from 'radashi'
const good = async (): Promise<number> => 1
const bad = async (): Promise<number> => { throw new Error('bad') }
const goodResult = await toResult(good())
// => [undefined, 1]
const badResult = await toResult(bad())
// => [Error('bad'), undefined]

@version12.4.0

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.

@seehttps://radashi.js.org/reference/async/toResult

@example

import { toResult, Result } from 'radashi'
const good = async (): Promise<number> => 1
const bad = async (): Promise<number> => { throw new Error('bad') }
const goodResult = await toResult(good())
// => [undefined, 1]
const badResult = await toResult(bad())
// => [Error('bad'), undefined]

@version12.4.0

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.

@seehttps://radashi.js.org/reference/async/toResult

@example

import { toResult, Result } from 'radashi'
const good = async (): Promise<number> => 1
const bad = async (): Promise<number> => { throw new Error('bad') }
const goodResult = await toResult(good())
// => [undefined, 1]
const badResult = await toResult(bad())
// => [Error('bad'), undefined]

@version12.4.0

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.

@seehttps://radashi.js.org/reference/async/toResult

@example

import { toResult, Result } from 'radashi'
const good = async (): Promise<number> => 1
const bad = async (): Promise<number> => { throw new Error('bad') }
const goodResult = await toResult(good())
// => [undefined, 1]
const badResult = await toResult(bad())
// => [Error('bad'), undefined]

@version12.4.0

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.

@paramreason The reason the promise was rejected.

@returnsA new rejected Promise.

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.

@seehttps://radashi.js.org/reference/async/toResult

@example

import { toResult, Result } from 'radashi'
const good = async (): Promise<number> => 1
const bad = async (): Promise<number> => { throw new Error('bad') }
const goodResult = await toResult(good())
// => [undefined, 1]
const badResult = await toResult(bad())
// => [Error('bad'), undefined]

@version12.4.0

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: ErrorConstructor
new (message?: string, options?: ErrorOptions) => Error (+1 overload)
Error
('bad')
}
return new
var Promise: PromiseConstructor
new <unknown>(executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void) => Promise<unknown>

Creates a new Promise.

@paramexecutor A callback used to initialize the promise. This callback is passed two arguments: a resolve callback used to resolve the promise with a value or the result of another promise, and a reject callback used to reject the promise with a provided reason or error.

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.

@seehttps://radashi.js.org/reference/async/toResult

@example

import { toResult, Result } from 'radashi'
const good = async (): Promise<number> => 1
const bad = async (): Promise<number> => { throw new Error('bad') }
const goodResult = await toResult(good())
// => [undefined, 1]
const badResult = await toResult(bad())
// => [Error('bad'), undefined]

@version12.4.0

toResult
(
function bad(): Promise<unknown>
bad
())
const res: Result<unknown>