@dfinity/assets
Version:
JavaScript and TypeScript library to manage assets on the Internet Computer
30 lines • 865 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.limit = void 0;
/**
* Minimal promise executor with concurrency limit implementation
* @param concurrency Maximum number of promises executed concurrently
*/
const limit = (concurrency) => {
const queue = [];
let active = 0;
const next = () => {
if (active < concurrency && queue.length > 0) {
active++;
const { fn, resolve, reject } = queue.shift() ?? {};
fn?.()
.then(resolve)
.catch(reject)
.then(() => {
active--;
next();
});
}
};
return (fn) => new Promise((resolve, reject) => {
queue.push({ fn, resolve, reject });
next();
});
};
exports.limit = limit;
//# sourceMappingURL=limit.js.map