Options
All
  • Public
  • Public/Protected
  • All
Menu

Module concurrency/utils

Functions

buildPromiseHolder

  • buildPromiseHolder<T>(): PromiseHolder<T>
  • Builds PromiseHolder instance.

    Type parameters

    • T

    Returns PromiseHolder<T>

    Promise holder.

runInSeries

  • runInSeries<I, O>(tasks: AsyncFunction<I, O>[], initialValue?: any): Promise<any[]>
  • Like Promise.all() but runs in series instead of parallel. Will pass the value of the prev func as the input to the next one, therefore a pipeline is simulated.

    Type parameters

    • I

    • O

    Parameters

    • tasks: AsyncFunction<I, O>[]

      Array with functions that return a promise.

    • Optional initialValue: any

      Initial value of the processing chain.

    Returns Promise<any[]>

    Results of the tasks.

synchronize

  • synchronize<T>(operation: AsyncFunction<void, T>): AsyncFunction<void, T>
  • Synchronizes operation and ensures that it won't be executed concurrently.
    Example:

    
    async function makeApiCall() {
        // function body
    }
                

    const nonConcurrentApiCallMaker = synchronize(makeApiCall);

    // makeApiCall will be called only once const results = await Promise.all([ nonConcurrentApiCallMaker(), nonConcurrentApiCallMaker() ]);

    expect(results[0]).to.be.eq(results[1]);

    Type parameters

    • T

    Parameters

    • operation: AsyncFunction<void, T>

    Returns AsyncFunction<void, T>

toPromise

  • toPromise<T>(maybePromise: Promise<T> | T): Promise<T>
  • Convert value into promise.

    Type parameters

    • T

    Parameters

    • maybePromise: Promise<T> | T

      Value or promise.

    Returns Promise<T>

    Converted promise.