link-checker-cli
Version:
CLI tool to check for broken links in a website or project
35 lines (34 loc) • 1.15 kB
JavaScript
class PromisePool {
constructor(options) {
this.results = [];
this.errors = [];
this.process = async () => {
const promises = new Set();
for (const item of this.items) {
const promise = this.handler(item)
.then((result) => {
this.results.push(result);
return result;
})
.catch((error) => {
this.errors.push(error);
throw error;
})
.finally(() => promises.delete(promise));
promises.add(promise);
if (promises.size >= this.concurrency) {
await Promise.race(promises);
}
}
await Promise.all(promises);
return { results: this.results, errors: this.errors };
};
this.items = options.items;
this.concurrency = options.concurrency;
this.handler = options.handler;
}
}
export function promisePool(options) {
const pool = new PromisePool(options);
return pool.process();
}