Function awaitableHandler

  • If the given value is a promise, the handler is called when is resolved, otherwise the handler is called directly with the value.

    Example

    const double = maybePromiseHandler((value: number) => value * 2);

    double(2); // 4
    double(Promise.resolve(2)); // Promise<4>

    Type Parameters

    • Value

    • Output

    Parameters

    • handler: ((awaitable) => Output)

      Handler function to be called with the value.

        • (awaitable): Output
        • Parameters

          • awaitable: Value

          Returns Output

    Returns (<AwaitableValue>(awaitable) => AwaitableValue extends Promise<Value>
        ? Promise<Output>
        : Output)

    Curried function with handler in context.

      • <AwaitableValue>(awaitable): AwaitableValue extends Promise<Value>
            ? Promise<Output>
            : Output
      • Type Parameters

        • AwaitableValue

        Parameters

        • awaitable: AwaitableValue

        Returns AwaitableValue extends Promise<Value>
            ? Promise<Output>
            : Output