UNPKG

@augment-vir/common

Version:

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

48 lines (47 loc) 2.67 kB
import { type NoInputsFunction, type PartialWithUndefined } from '@augment-vir/core'; /** * 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> = PartialWithUndefined<{ /** * 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?: FallbackValue; }): Promise<Awaited<FallbackValue> | Awaited<Value>>; export declare function wrapInTry<Value, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: { handleError: (error: unknown) => FallbackValue; fallbackValue?: FallbackValue; }): FallbackValue | Value; export declare function wrapInTry<Value extends Promise<any>, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: { handleError?: ((error: unknown) => FallbackValue) | undefined; fallbackValue: FallbackValue; }): Promise<Awaited<FallbackValue> | Awaited<Value>>; export declare function wrapInTry<Value, FallbackValue = undefined>(callback: NoInputsFunction<Value>, options: { handleError?: ((error: unknown) => FallbackValue) | undefined; 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;