async-delay-queue
Minimal ES7 async queue with dynamic delay between functions.
Usage
queue
Returns a promise resolving the return value of the given function.
Argument | Description | Default |
---|---|---|
fn | Function to delay. Supports promise/async functions. | None |
delay | Delay between this function and the last one in ms. Required. | null |
timer | Max time until the active promise is forcefully resolved to prevent the queue getting stuck. | null |
add | Method with which the function is added to the queue stack. "unshift" to add to start, "push" to add to end of queue. | "push" |
Example
Crawl Google.com and retry when hitting rate limits.
const queue = const request = // for example purposes only async { let res = await queue // Hit rate limits? Put the same request at the start of the queue. // "unshift" to push at start, and increase delay to 10s. if resstatusCode === 429 res = await queue return res} // Crawls google.com 20 times with a 100ms delay between each request.for let i = 0; i < 20; i++