Function funAsyncRateLimit

  • Function call rate limiter / concurrency control.

    Type Parameters

    • QueueType extends IQueue<(() => Promise<unknown>), EnqueueOptionsType>

    • EnqueueOptionsType extends IQueueAddOptions

    Parameters

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

      Function to be rate limited.

        • (...args): any
        • Parameters

          • Rest ...args: any[]

          Returns any

    • Optional options: IFunAsyncRateLimitOptions

    Returns [queue: PromiseQueue<QueueType, EnqueueOptionsType>, wrapped: ((...args) => Promise<any>)]

    See

    IFunAsyncRateLimitOptions

    Example

    const [queue, waitSecondsLimited] = funAsyncRateLimit(waitSeconds, {
    // Whether the task must finish in the given interval or will be carried over into the next interval count.
    carryoverConcurrencyCount: false,
    // max 50
    intervalCap: 50,
    // per minute
    interval: 1000 * 60,
    // max 3 concurrent
    concurrency: 3,
    // whether to start immediately when pushed to queue
    autoStart: true,
    priority: 0,
    timeout: 0,
    throwOnTimeout: false,
    })

    queue.onEmpty().then(() => {
    console.log('Queue empty')
    })

    for (let i = 0; i < 7; i++) {
    console.log({ i, inQueueBefore: queue.size })
    waitSecondsLimited(2).then((returnValue) => {
    console.log({ i, inQueueAfter: queue.size, returnValue })
    })
    }
    //=> { i: 0, inQueueBefore: 0 }
    //=> { i: 1, inQueueBefore: 0 }
    //=> { i: 2, inQueueBefore: 0 }
    //=> { i: 3, inQueueBefore: 0 }
    //=> { i: 4, inQueueBefore: 1 }
    //=> { i: 5, inQueueBefore: 2 }
    //=> { i: 6, inQueueBefore: 3 }
    //=> Queue empty
    //=> { i: 0, inQueueAfter: 3, returnValue: undefined }
    //=> { i: 1, inQueueAfter: 2, returnValue: undefined }
    //=> { i: 2, inQueueAfter: 1, returnValue: undefined }
    //=> { i: 3, inQueueAfter: 0, returnValue: undefined }
    //=> { i: 4, inQueueAfter: 0, returnValue: undefined }
    //=> { i: 5, inQueueAfter: 0, returnValue: undefined }
    //=> { i: 6, inQueueAfter: 0, returnValue: undefined }

Generated using TypeDoc