@atomist/automation-client
Version:
Atomist API for software low-level client
49 lines • 2.24 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const configuration_1 = require("../configuration");
function concurrentDefault() {
return configuration_1.configurationValue("pool.concurrent", 5);
}
/**
* Execute all provided promises with a max concurrency
* Results will be in the same order as the provided promises; if one promise rejects,
* execution is stopped and the returned promise is rejected as well.
* @param promises all promises to execute
* @param concurrent the max number of concurrent promise executions
*/
function executeAll(promises, concurrent = concurrentDefault()) {
return __awaiter(this, void 0, void 0, function* () {
let index = 0;
const results = [];
const producer = () => {
if (index < promises.length) {
const promise = promises[index]();
results[index] = promise;
index++;
return promise;
}
else {
// tslint:disable-next-line:no-null-keyword
return null;
}
};
const PromisePool = require("es6-promise-pool");
const pool = new PromisePool(producer, concurrent);
pool.addEventListener("fulfilled", (event) => {
results[results.indexOf(event.data.promise)] = event.data.result;
});
yield pool.start(); // start only returns a promise; not an [] of results
return results;
});
}
exports.executeAll = executeAll;
//# sourceMappingURL=pool.js.map