async-delay-queue
Minimal ES7 async queue with dynamic delay between functions.
Usage
queue.delay(fn, delay, timer, add)
Returns a promise resolving the return value of the given function.
| 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 = require("async-delay-queue")
const request = require("request-promise")
async crawl(url) {
let res = await queue.delay(() => request(url), 100)
if (res.statusCode === 429) {
res = await queue.delay(() => request(url), 10000, null, "unshift")
}
return res
}
for (let i = 0; i < 20; i++) {
crawl("http://google.com")
}