UNPKG

@lx-frontend/multi-track

Version:

sls 日志上传功能,当前支持小程序、web

54 lines 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Concurrent = void 0; /** * @example 并发控制类 * * const concurrent = new Concurrent(3); // 3个并发 * * // 模拟10个异步任务 * for (let i = 0; i < 10; i++) { * concurrent.append(() => { * return new Promise((resolve) => { * console.log(`开始任务 ${i}`) * setTimeout(() => { * console.log(`结束任务 ${i}`) * resolve(true) * }, 1000) * }) * }) * } * */ var Concurrent = /** @class */ (function () { function Concurrent(total) { this.total = total; this.count = 0; this.peddings = []; } Concurrent.prototype.append = function (fn) { this.peddings.push(fn); this.run(); }; Concurrent.prototype.run = function () { var _this = this; if (!this.peddings.length) { return; } if (this.count >= this.total) { return; } var fn = this.peddings.shift(); if (!fn) { return; } this.count++; fn().finally(function () { _this.count--; _this.run(); }); }; return Concurrent; }()); exports.Concurrent = Concurrent; //# sourceMappingURL=Concurrent.js.map