Created
June 13, 2020 10:04
-
-
Save mmomtchev/925a4130c1493256ac9c31c6bc26b147 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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