"use strict";
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
    if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
    var g = generator.apply(thisArg, _arguments || []), i, q = [];
    return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
    function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
    function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
    function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
    function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
    function fulfill(value) { resume("next", value); }
    function reject(value) { resume("throw", value); }
    function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.batchTasks = batchTasks;
function batchTasks(tasks_1, limit_1) {
    return __asyncGenerator(this, arguments, function* batchTasks_1(tasks, limit, taskCallback = (r) => r) {
        // iterate over tasks
        for (let i = 0; i < tasks.length; i = i + limit) {
            // grab the batch of tasks for current iteration
            const batch = tasks.slice(i, i + limit);
            // wait for them to resolve concurrently
            const result = yield __await(Promise.all(
            // optionally attach callback to perform any side effects
            batch.map((task) => task().then((r) => taskCallback(r)))));
            // yield the batched result and let consumer know
            yield yield __await(result);
        }
    });
}
//# sourceMappingURL=promises.js.map