UNPKG

@shanyue/promise-utils

Version:

Userful promise utils, include map, filter, retry and sleep

38 lines 947 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Limit = void 0; class Limit { constructor(limit) { this.limit = limit; this.count = 0; this.queue = []; } enqueue(fn) { return new Promise((resolve, reject) => { this.queue.push({ fn, resolve, reject }); }); } dequeue() { if (this.count < this.limit && this.queue.length) { const { fn, resolve, reject } = this.queue.shift(); this.run(fn).then(resolve).catch(reject); } } async run(fn) { this.count++; const value = await fn(); this.count--; this.dequeue(); return value; } build(fn) { if (this.count < this.limit) { return this.run(fn); } else { return this.enqueue(fn); } } } exports.Limit = Limit; //# sourceMappingURL=limit.js.map