Skip to content

Instantly share code, notes, and snippets.

@mmomtchev
Created June 13, 2020 10:04
Show Gist options
  • Save mmomtchev/925a4130c1493256ac9c31c6bc26b147 to your computer and use it in GitHub Desktop.
Save mmomtchev/925a4130c1493256ac9c31c6bc26b147 to your computer and use it in GitHub Desktop.
const Queue = require('async-await-queue');
/**
* No more than 10 parallel, spaced at least 100ms apart
* These are typical fair-use limitations of public APIs
**/
const queue = new Queue(10, 100);
let p = [];
for (let url of urls) {
/* Generate a queue ID */
const me = Symbol();
/* 0 is the priority, -1 is higher priority than 0 */
p.push(queue.wait(me, 0)
.then(() => download(url))
.then((html) => {
data[url] = parse(html);
})
.catch((e) => console.error(e))
/* don't forget this or you will end up freezing */
.finally(() => queue.end(me)));
}
await Promise.allSettled(p);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment