Skip to main content

tryCall

Callable

  • tryCall<T, TResult1, TResult2>(block: () => Awaitable<T>, onSuccess: undefined | (value: T) => Awaitable<TResult1>, onError?: (error: unknown) => Awaitable<TResult2>): Awaitable<TResult1 | TResult2>

  • Returns block().then(onSuccess, onError) when a asynchronous. Else returns try { return onSuccess(block()) } catch (error) { onError(error) } when synchronous

    @example
    const syncBlock = () => 'sync'
    tryCall(syncBlock, (_) => `${_}_foo`);// 'async_foo'
    const asyncBlock = () => Promise.resolve('async')
    tryCall(asyncBlock, (_) => `${_}_foo`);// Promise.resolve('async_foo')

    Type parameters

    • T
    • TResult1 = T
    • TResult2 = never