Options
All
  • Public
  • Public/Protected
  • All
Menu

async-wrappers

Wrapper Functions

  • debounce - Only call a function after no calls have ocurred for a specified interval.
  • throttle - Don't call a function faster than a specified interval.
  • retry - Attempt to call a function until it succeeds.
  • wait Wait specified time for a value.

Argument Reducers

A set of premade reducer functions are included. These can be used by the reducer option on debounce and throttle to determine what the arguments to the wrapped function should be.

  • latest - Uses only the latest called arguments.
  • combine - Combines all arguments into a single array.
  • calls - Combines all calls into an array of call arguments.
  • extend - Combines call arguments by concatenating them into a set of arguments.

Index

Retry Type aliases

RetryArguments

RetryArguments<F>: Parameters<F> | RetryArgumentsCallback<F>

An array of arguments or callback for retry to use when calling it's function

Type parameters

  • F: (...args: any[]) => any

RetryArgumentsCallback

RetryArgumentsCallback<RetryFunction>: (attempt: number, delay: number, error: any) => Parameters<RetryFunction> | Promise<Parameters<RetryFunction>>

callback for retry to determine the arguments to the function to retry.

Type parameters

  • RetryFunction: (...args: any[]) => any

    the type of function to retry.

Type declaration

    • (attempt: number, delay: number, error: any): Parameters<RetryFunction> | Promise<Parameters<RetryFunction>>
    • Parameters

      • attempt: number

        The attempt number for the next try.

      • delay: number

        The current delay before retrying.

      • error: any

        The error from the current attempt.

      Returns Parameters<RetryFunction> | Promise<Parameters<RetryFunction>>

      The arguments for the function

RetryDelay

RetryDelay: RetryDelayCallback | number[] | number

A delay, list of delays or [[RetryNextDelayFunction|callback]] for retry to use when determining the delay between attempts.

RetryDelayCallback

RetryDelayCallback: (attempt: number, previousDelay: number, error: any) => number | PromiseLike<number>

Function for determining a how long to wait after the given attempt

Type declaration

    • (attempt: number, previousDelay: number, error: any): number | PromiseLike<number>
    • Parameters

      • attempt: number

        The current attempt number.

      • previousDelay: number

        The previous delay.

      • error: any

        The error from the current attempt.

      Returns number | PromiseLike<number>

      The new delay in milliseconds.

RetryErrorTypes

RetryErrorTypes: "cancelled" | "stopped"

Types of retry errors

RetryStop

RetryStop: RetryStopCallback | number

The number of calls or callback for retry to use when determining to stop retrying.

RetryStopCallback

RetryStopCallback: (nextAttempt: number, delay: number, error: any) => boolean | string

A function used to determine if to stop for the given retry attempt.

Errors thrown by this function will cause retry to reject with the errorTypes of retry errors

Type declaration

    • (nextAttempt: number, delay: number, error: any): boolean | string
    • Parameters

      • nextAttempt: number

        The attempt number for the next try.

      • delay: number

        The current delay before retrying.

      • error: any

        The error from the current attempt.

      Returns boolean | string

      true or a message to stop trying and reject with the given message.

Wait Type aliases

WaitValue

WaitValue<T>: T | PromiseLike<T> | WaitValueCallback<T>

A value or callback that will return the value for wait.

Type parameters

  • T

    the type of the value.

WaitValueCallback

WaitValueCallback<T>: () => T | PromiseLike<T>

A callback that will return a value after wait is completed.

Type parameters

  • T

    the type of the value.

Type declaration

    • (): T | PromiseLike<T>
    • Returns T | PromiseLike<T>

      the value or promise to return the value

Argument Reducer Type aliases

ArgumentsReducer

ArgumentsReducer<InvokeArgs, CallArgs>: (invokeArgs: InvokeArgs, callArgs: CallArgs) => InvokeArgs

A reducer used to create the invocation arguments for a function over multiple calls.

It receives the current invocation arguments and the call arguments and returns the new invocation arguments.

Type parameters

  • InvokeArgs: any[]

  • CallArgs: any[]

Type declaration

    • (invokeArgs: InvokeArgs, callArgs: CallArgs): InvokeArgs
    • Parameters

      • invokeArgs: InvokeArgs

        The current invocation arguments for the main function. The first call will be an empty array.

      • callArgs: CallArgs

        The arguments given to the latest call.

      Returns InvokeArgs

      The new invocation arguments.

ReducerFunction

ReducerFunction<InvokeFunc, CallFuncOrArgs>: ArgumentsReducer<Parameters<InvokeFunc>, ParametersOrArray<CallFuncOrArgs>>

An implementation ArgumentsReducer for a particular invocation function.

Type parameters

  • InvokeFunc: (...args: any[]) => any

    the type of the invocation function.

  • CallFuncOrArgs: (...args: any[]) => any | any[]

    the arguments or type of the call function.

Wrapper Functions

Const debounce

  • Ensure multiple calls to a function will only execute it when it has been inactive (no more calls) for a specified delay.

    Execution always happens asynchronously.

    If the delay is 0 execution will be scheduled to occur after the current runtime event loop.

    The debounced function returns a promise that resolves to the return value of func.

    By default the arguments to func are the latest arguments given to the debounced function. For custom behaviour pass an argumentsReducer function.

    Type parameters

    Parameters

    • fn: Invoke

      The function to debounce

    • Default value delay: number = 50

      The number of milliseconds on inactivity before the function will be executed. .

    • Default value options: DebounceOptions<Reducer> = {}

    Returns Debounced<Invoke, Reducer>

    the debounced function

Const retry

  • Retry the wrapped function according to the

    Parameters

    • func: (...args: any[]) => any

      the function to retry.

        • (...args: any[]): any
        • Parameters

          • Rest ...args: any[]

          Returns any

    • Default value delay: RetryDelay = defaultDelays

      a delay in milliseconds, an array of millisecond delays or callback to determine the delay before the next attempt.

    • Default value stop: RetryStop = 10

      the number of attempts, or callback to determine when retry should stop retrying func.

    • Default value args: RetryArguments<typeof func> = ([] as unknown) as Parameters<typeof func>

      an array or callback to provide arguments to func

    Returns RetryResult<typeof func>

Const throttle

  • throttle<Invoke, Reducer>(func: Invoke, delay?: number, options?: ThrottleOptions<Reducer>): Throttled<Invoke, Reducer>
  • Ensure multiple calls to a function will only execute at most once every delay milliseconds.

    The execution will always happens asynchronously. If the delay is 0 then execution will occur after the current runtime event loop.

    The throttled function returns a promise that resolves to the return value of func.

    By default the arguments to func are the latest arguments given to the throttled function. For custom behaviour pass an argumentsReducer function.

    Type parameters

    Parameters

    • func: Invoke

      The function to throttle

    • Default value delay: number = 50

      The number of milliseconds between executions.

    • Default value options: ThrottleOptions<Reducer> = {}

      Debounce options

    Returns Throttled<Invoke, Reducer>

    The throttled function

Const wait

  • Resolves after a given delay, optionally with a value.

    If the delay is 0 the promises will resolve after the current runtime event loop.

    Type parameters

    • T

    Parameters

    • delay: number

      The time in milliseconds before the value is returned.

    • Optional func: WaitValue<T>

      A function or value that will be returned after waiting.

    Returns WaitResult<T>

    A promise that resolves with the value/function result. The promise has 2 extra functions defined:

    • cancel cancels the result and rejects the promise.
    • stop stops waiting and resolves the promised value.

Argument Reducer Functions

Const callArguments

  • callArguments<T>(invokeArgs: [T[]], callArgs: T): [T[]]
  • Reducer that provices the invocation function with a single array of arrays of all calls.

    // the sequence of calls below to a call function
    call(1);
    call(1,2,3);
    call(4,5);
    call([1]);
    call('last');
    // would be the equivalent to the invoke call below.
    invoke([[1], [1, 2, 3], [4, 5], [[1]], ['last']]);

    Type parameters

    • T: any

    Parameters

    • invokeArgs: [T[]]
    • callArgs: T

    Returns [T[]]

Const combineArguments

  • combineArguments<T>(invokeArgs: [T[]], callArgs: T[]): [T[]]
  • An arguments reducer that takes combines all arguments from every call function into an array that is used as the first argument to the invoke function.

    Example:

    // the sequence of calls below to a call function
    call(1);
    call(1,2,3);
    call(4,5);
    call([1]);
    call('last');
    // would be the equivalent to the invoke call below.
    invoke([1, 1, 2, 3, 4, 5, [1], 'last']);

    Type parameters

    • T: any

    Parameters

    • invokeArgs: [T[]]
    • callArgs: T[]

    Returns [T[]]

Const extendArguments

  • extendArguments<T>(invokeArgs: T[], callArgs: T[]): T[]
  • Reducer that extends the invocation arguments array with all arguments given to it.

    Example:

    // the sequence of calls below to a call function
    call(1);
    call(1,2,3);
    call(4,5);
    call([1]);
    call('last');
    // would be the equivalent to the invoke call below.
    invoke(1, 1, 2, 3, 4, 5, [1], 'last');

    Type parameters

    • T: any

    Parameters

    • invokeArgs: T[]
    • callArgs: T[]

    Returns T[]

Const latestArguments

  • latestArguments<T>(invokeArgs: T, callArgs: T): T
  • An arguments reducer that will use the latest call arguments as the invocation arguments.

    Example:

    // the sequence of calls below to a call function
    call(1);
    call(1,2,3);
    call(4,5);
    call([1]);
    call('last');
    // would be the equivalent to the invoke call below.
    invoke('last');

    Type parameters

    • T: any[]

    Parameters

    • invokeArgs: T
    • callArgs: T

    Returns T

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc