Variable concurrencyConst

concurrency: {
    any: (<T>(iterable) => Promise<T>);
    debounce: (<T>(runner, wait) => T);
    limit: (<T>(runner, concurrencyNumber) => T);
    reuse: (<T>(runner, duration?, maxHandles?) => T & {
        __wrap_global__: {
            duration: number;
            handles: LRUMap<string, {
                timeout: number;
                value: any;
            }>;
        };
    });
    series: (<T>(...asyncOperations) => Promise<SeriesResult<T>>);
    synchronized: (<T>(func) => T);
    timeout: (<T>(runner, timeout?) => T);
} = ...

concurrency functions

Type declaration

  • any: (<T>(iterable) => Promise<T>)
      • <T>(iterable): Promise<T>
      • Promise.any implementation

        just ref the MDN document

        Promise.any() takes an iterable of Promise objects and, as soon as one of the promises in the iterable fulfils, returns a single promise that resolves with the value from that promise. If no promises in the iterable fulfil (if all of the given promises are rejected), then throw the array of errors

        Type Parameters

        • T

        Parameters

        • iterable: Iterable<Promise<T>>

        Returns Promise<T>

        Since

        5.7.0

        Throws

        Error list

  • debounce: (<T>(runner, wait) => T)
      • <T>(runner, wait): T
      • concurrency simply debounce function

        it will create a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked

        Type Parameters

        Parameters

        • runner: T
        • wait: number

          wait milliseconds before last time invocation

        Returns T

        Since

        5.18.0

  • limit: (<T>(runner, concurrencyNumber) => T)
      • <T>(runner, concurrencyNumber): T
      • limit concurrent for parallel operations

        Type Parameters

        Parameters

        • runner: T

          async operation function

        • concurrencyNumber: number

          max concurrency number

        Returns T

        the concurrency limited function wrapper

        Since

        5.15.0

  • reuse: (<T>(runner, duration?, maxHandles?) => T & {
        __wrap_global__: {
            duration: number;
            handles: LRUMap<string, {
                timeout: number;
                value: any;
            }>;
        };
    })
      • <T>(runner, duration?, maxHandles?): T & {
            __wrap_global__: {
                duration: number;
                handles: LRUMap<string, {
                    timeout: number;
                    value: any;
                }>;
            };
        }
      • reuse values in specific duration for async functions

        Type Parameters

        Parameters

        • runner: T

          must be an async function

        • duration: number = 1000

          default 1000 milliseconds

        • maxHandles: number = 1000

          different cache values for parameters

        Returns T & {
            __wrap_global__: {
                duration: number;
                handles: LRUMap<string, {
                    timeout: number;
                    value: any;
                }>;
            };
        }

        Since

        5.22.0

  • series: (<T>(...asyncOperations) => Promise<SeriesResult<T>>)
      • <T>(...asyncOperations): Promise<SeriesResult<T>>
      • run async operations one by one, serially

        and return the result array

        if any operation raise error, the following operations will not be executed

        Type Parameters

        Parameters

        • Rest ...asyncOperations: T

          async operations

          Rest

        Returns Promise<SeriesResult<T>>

        Since

        5.14.0

        Example

        const [res1, res2, res3] = await series(
        () => fetch(1),
        () => fetch(2),
        () => fetch(3)
        )
  • synchronized: (<T>(func) => T)
      • <T>(func): T
      • let async function only invoke at once in same time

        Type Parameters

        Parameters

        • func: T

          the function to be processed

        Returns T

        the wrapped function instance

        Since

        5.20.0

  • timeout: (<T>(runner, timeout?) => T)
      • <T>(runner, timeout?): T
      • wrap an async function with timeout

        Type Parameters

        Parameters

        • runner: T

          async runner please, otherwise the timeout is not meaningful

        • timeout: number = ...

          timeout threshold in milliseconds, default value is 60 seconds

        Returns T

        Since

        5.15.0

        Throws

        Example

        const f = timeout(async() => {}, 1000)
        // f() will throw error if the result is not resolved in one second