Options
All
  • Public
  • Public/Protected
  • All
Menu

promise-utils

Index

Functions

delay

  • delay<T>(delayTimeMs: number, value: T): Promise<T>
  • delay<T>(delayTimeMs: number): Promise<void>
  • Optionally returns a value after a delay. This is useful if you want to add jitter or need to wait for some external reason.

    optionalparam

    {any} value - the value to return after the delay

    Type parameters

    • T

    Parameters

    • delayTimeMs: number
    • value: T

    Returns Promise<T>

    value - if defined

  • Type parameters

    • T

    Parameters

    • delayTimeMs: number

    Returns Promise<void>

filter

  • filter<T, V>(input: T[], predicate: function): Promise<T[]>
  • filter<T, V>(input: T[], predicate: function): Promise<T[]>
  • filter<T, V>(input: T, predicate: function): Promise<T[keyof T][]>
  • filter<T, V>(input: T, predicate: function): Promise<T[keyof T][]>
  • Returns a new array of all the values in coll which pass an async truth test. This operation is performed in parallel, but the results array will be in the same order as the original.

    Type parameters

    • T

    • V

    Parameters

    • input: T[]
    • predicate: function

      A truth test to apply to each item in coll. Invoked with (item, key/index), must return a boolean.

        • (value: T, index: number): Promise<V>
        • Parameters

          • value: T
          • index: number

          Returns Promise<V>

    Returns Promise<T[]>

    The filtered collection

  • Type parameters

    • T

    • V

    Parameters

    • input: T[]
    • predicate: function
        • (value: T): Promise<V>
        • Parameters

          • value: T

          Returns Promise<V>

    Returns Promise<T[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • predicate: function
        • (value: T[keyof T], key: keyof T): Promise<V>
        • Parameters

          • value: T[keyof T]
          • key: keyof T

          Returns Promise<V>

    Returns Promise<T[keyof T][]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • predicate: function
        • (value: T[keyof T]): Promise<V>
        • Parameters

          • value: T[keyof T]

          Returns Promise<V>

    Returns Promise<T[keyof T][]>

flatMap

  • flatMap<T, V>(input: T[], iteratee: function): Promise<V[]>
  • flatMap<T, V>(input: T[], iteratee: function): Promise<V[]>
  • flatMap<T, V>(input: T, iteratee: function): Promise<V[]>
  • flatMap<T, V>(input: T, iteratee: function): Promise<V[]>
  • The same as map but will flatten the results.

    Type parameters

    • T

    • V

    Parameters

    • input: T[]

      A collection to iterate over.

    • iteratee: function

      An async function to apply to each item in coll. The iteratee should complete with the transformed item. Invoked with (item, key).

        • (value: T, index: number): Promise<V | V[]>
        • Parameters

          • value: T
          • index: number

          Returns Promise<V | V[]>

    Returns Promise<V[]>

  • Type parameters

    • T

    • V

    Parameters

    • input: T[]
    • iteratee: function
        • (value: T): Promise<V | V[]>
        • Parameters

          • value: T

          Returns Promise<V | V[]>

    Returns Promise<V[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • iteratee: function
        • (value: T[keyof T], key: string): Promise<V | V[]>
        • Parameters

          • value: T[keyof T]
          • key: string

          Returns Promise<V | V[]>

    Returns Promise<V[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • iteratee: function
        • (value: T[keyof T]): Promise<V | V[]>
        • Parameters

          • value: T[keyof T]

          Returns Promise<V | V[]>

    Returns Promise<V[]>

immediate

  • immediate<T>(value: T): Promise<T>
  • immediate<T>(): Promise<void>
  • Optionally returns a value after deferring execution. This is useful if you need to wait for anything left on the event loop.

    optionalparam

    {any} value - the value to return

    Type parameters

    • T

    Parameters

    • value: T

    Returns Promise<T>

    value - if defined

  • Type parameters

    • T

    Returns Promise<void>

invert

  • invert<T>(promise: Promise<T>, message?: undefined | string): Promise<any>
  • Inverts the given promise - i.e. throws an error if it completes successfully and returns the error if it throws one.

    Type parameters

    • T

    Parameters

    • promise: Promise<T>

      the promise to invert

    • Optional message: undefined | string

      the message to throw if the promise resolves

    Returns Promise<any>

    the error thrown by the promise

map

  • map<T, V>(input: T[], iteratee: function): Promise<V[]>
  • map<T, V>(input: T[], iteratee: function): Promise<V[]>
  • map<T, V>(input: T, iteratee: function): Promise<V[]>
  • map<T, V>(input: T, iteratee: function): Promise<V[]>
  • Produces a new collection of values by mapping each value in coll through the iteratee function. The iteratee is called with an item from coll and a callback for when it has finished processing. Each of these callback takes 2 arguments: an error, and the transformed item from coll. If iteratee passes an error to its callback, the main callback (for the map function) is immediately called with the error.

    Note, that since this function applies the iteratee to each item in parallel, there is no guarantee that the iteratee functions will complete in order. However, the results array will be in the same order as the original coll.

    Type parameters

    • T

    • V

    Parameters

    • input: T[]

      A collection to iterate over.

    • iteratee: function

      An async function to apply to each item in coll. The iteratee should return the transformed item. Invoked with (item, key).

        • (value: T, index: number): Promise<V>
        • Parameters

          • value: T
          • index: number

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T

    • V

    Parameters

    • input: T[]
    • iteratee: function
        • (value: T): Promise<V>
        • Parameters

          • value: T

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • iteratee: function
        • (value: T[keyof T], key: string): Promise<V>
        • Parameters

          • value: T[keyof T]
          • key: string

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • iteratee: function
        • (value: T[keyof T]): Promise<V>
        • Parameters

          • value: T[keyof T]

          Returns Promise<V>

    Returns Promise<V[]>

mapLimit

  • mapLimit<T, V>(input: T[], limit: number, iteratee: function): Promise<V[]>
  • mapLimit<T, V>(input: T[], limit: number, iteratee: function): Promise<V[]>
  • mapLimit<T, V>(input: T, limit: number, iteratee: function): Promise<V[]>
  • mapLimit<T, V>(input: T, limit: number, iteratee: function): Promise<V[]>
  • The same as map but runs a maximum of limit async operations at a time. Also does not have the same ordering guarantees.

    Type parameters

    • T

    • V

    Parameters

    • input: T[]

      A collection to iterate over.

    • limit: number

      The maximum number of async operations at a time.

    • iteratee: function

      An async function to apply to each item in coll. The iteratee should complete with the transformed item. Invoked with (item, key).

        • (value: T, index: number): Promise<V>
        • Parameters

          • value: T
          • index: number

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T

    • V

    Parameters

    • input: T[]
    • limit: number
    • iteratee: function
        • (value: T): Promise<V>
        • Parameters

          • value: T

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • limit: number
    • iteratee: function
        • (value: T[keyof T], key: string): Promise<V>
        • Parameters

          • value: T[keyof T]
          • key: string

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • limit: number
    • iteratee: function
        • (value: T[keyof T]): Promise<V>
        • Parameters

          • value: T[keyof T]

          Returns Promise<V>

    Returns Promise<V[]>

mapSeries

  • mapSeries<T, V>(input: T[], iteratee: function): Promise<V[]>
  • mapSeries<T, V>(input: T[], iteratee: function): Promise<V[]>
  • mapSeries<T, V>(input: T, iteratee: function): Promise<V[]>
  • mapSeries<T, V>(input: T, iteratee: function): Promise<V[]>
  • The same as mapLimit but with only 1 operation at a time, and maintains the ordering guarantees of map.

    Type parameters

    • T

    • V

    Parameters

    • input: T[]

      A collection to iterate over.

    • iteratee: function

      An async function to apply to each item in coll. The iteratee should complete with the transformed item. Invoked with (item, key).

        • (value: T, index: number): Promise<V>
        • Parameters

          • value: T
          • index: number

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T

    • V

    Parameters

    • input: T[]
    • iteratee: function
        • (value: T): Promise<V>
        • Parameters

          • value: T

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • iteratee: function
        • (value: T[keyof T], key: string): Promise<V>
        • Parameters

          • value: T[keyof T]
          • key: string

          Returns Promise<V>

    Returns Promise<V[]>

  • Type parameters

    • T: Object

    • V

    Parameters

    • input: T
    • iteratee: function
        • (value: T[keyof T]): Promise<V>
        • Parameters

          • value: T[keyof T]

          Returns Promise<V>

    Returns Promise<V[]>

memoize

  • memoize<FnType>(fn: FnType, hasher?: Function, timeoutMs?: undefined | number): FnType
  • Caches the results of an async function. When creating a hash to store function results against, the callback is omitted from the hash and an optional hash function can be used.

    If no hash function is specified, the first argument is used as a hash key, which may work reasonably if it is a string or a data type that converts to a distinct string. Note that objects and arrays will not behave reasonably. Neither will cases where the other arguments are significant. In such cases, specify your own hash function.

    Type parameters

    • FnType: Function

    Parameters

    • fn: FnType

      The async function to proxy and cache results from.

    • Default value hasher: Function = _.identity

      An optional function for generating a custom hash for storing results. It has all the arguments applied to it and must be synchronous.

    • Optional timeoutMs: undefined | number

    Returns FnType

    a memoized version of fn

retry

  • Attempts to get a successful response from task no more than times times before returning an error. If the task is successful, the callback will be passed the result of the successful task. If all attempts fail, the callback will be passed the error and result (if any) of the final attempt.

    Type parameters

    • T: Function

    Parameters

    • fn: T

      An async function to retry.

    • retryOpts: RetryOpts

    Returns T

    A wrapped version of function that performs retries

settleAll

  • settleAll<T, V>(promises: Promise<T>[], errFn?: function): Promise<object>
  • Attempts to settle all promises in promises in parallel, calling errFn when a promise rejects. Similar to Promise.all, but does not fail fast. For resolved promises, the result array contains the resolved value at the same index as the promise. For rejected promises, the result array contains the return value of errFn at the same index as the promise.

    Type parameters

    • T

    • V

    Parameters

    • promises: Promise<T>[]

      An array of promises to attempt to settle.

    • Default value errFn: function = _.identity

      The function to call when a promise rejects.

        • (err: Error): V
        • Parameters

          • err: Error

          Returns V

    Returns Promise<object>

    A list of resolved and rejected values of promises.

timeout

  • timeout<T>(fn: T, expirationTime: number, errorMessage?: undefined | string): T
  • Sets a time limit on an asynchronous function. If the function does return callback within the specified milliseconds, it will be called with a timeout error.

    Type parameters

    • T: Function

    Parameters

    • fn: T

      The async function to limit in time.

    • expirationTime: number

      The specified time limit.

    • Optional errorMessage: undefined | string

      The message that should be sent if the function times out

    Returns T

    Returns a wrapped function that will throw an error if it takes too long. Invoke this function with the same parameters as you would fn.

Generated using TypeDoc