UNPKG

@augment-vir/common

Version:

A collection of augments, helpers types, functions, and classes for any JavaScript environment.

49 lines (48 loc) 2.59 kB
import { type NoInputsFunction } from '@augment-vir/core'; import { type RequireOneOrNone } from 'type-fest'; /** * Options for {@link wrapInTry}. * * @category Function * @category Package : @augment-vir/common * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common) */ export type WrapInTryOptions<FallbackValue> = RequireOneOrNone<{ /** * Call this function if the callback passed to {@link wrapInTry} throws an error. The thrown * error is passed to this function. If a `fallbackValue` option is also provided, it will be * ignored. */ handleError: (error: unknown) => FallbackValue; /** * Fallback to this value if the callback passed to {@link wrapInTry} throws an error. This will * be ignored if a `handleError` option is also set. */ fallbackValue: FallbackValue; }>; export declare function wrapInTry<Value extends Promise<any>>(callback: NoInputsFunction<Value>, options?: undefined | { handleError?: never; fallbackValue?: never; }): Promise<Error | Awaited<Value>>; export declare function wrapInTry<Value>(callback: NoInputsFunction<Value>, options?: undefined | { handleError?: never; fallbackValue?: never; }): Error | Value; export declare function wrapInTry<Value extends Promise<any>, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: { handleError: (error: unknown) => FallbackValue; fallbackValue?: never; }): Promise<Awaited<FallbackValue> | Awaited<Value>>; export declare function wrapInTry<Value, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: { handleError: (error: unknown) => FallbackValue; fallbackValue?: never; }): FallbackValue | Value; export declare function wrapInTry<Value extends Promise<any>, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: { handleError?: never; fallbackValue: FallbackValue; }): Promise<Awaited<FallbackValue> | Awaited<Value>>; export declare function wrapInTry<Value, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: { handleError?: never; fallbackValue: FallbackValue; }): FallbackValue | Value; export declare function wrapInTry<Value extends Promise<any>, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options?: WrapInTryOptions<FallbackValue> | undefined): Promise<FallbackValue | Value | Error>; export declare function wrapInTry<Value, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options?: WrapInTryOptions<FallbackValue> | undefined): FallbackValue | Value | Error;