ConcurrentCaller tweaks

This commit is contained in:
Dan Stillman 2015-01-28 17:28:38 -05:00
parent 8499ec6211
commit 84d1be4c03

View File

@ -89,8 +89,7 @@ ConcurrentCaller.prototype.pause = function (ms) {
ConcurrentCaller.prototype.fcall = function (func) { ConcurrentCaller.prototype.fcall = function (func) {
if (Array.isArray(func)) { if (Array.isArray(func)) {
var promises = []; var promises = [];
for (var i in func) { for (let i = 0; i < func.length; i++) {
//this._log("Running fcall on function");
promises.push(this.fcall(func[i])); promises.push(this.fcall(func[i]));
} }
return Promise.settle(promises); return Promise.settle(promises);
@ -117,14 +116,14 @@ ConcurrentCaller.prototype.fcall = function (func) {
ConcurrentCaller.prototype.stop = function () { ConcurrentCaller.prototype.stop = function () {
self._log("Clearing queue"); this._log("Clearing queue");
self._queue = []; this._queue = [];
}; };
ConcurrentCaller.prototype._onFunctionDone = function (promise) { ConcurrentCaller.prototype._onFunctionDone = function (promise) {
var self = this; var self = this;
return promise.then(function (promise) { return promise.then(function (result) {
self._numRunning--; self._numRunning--;
self._log("Done with function (" self._log("Done with function ("
@ -142,6 +141,7 @@ ConcurrentCaller.prototype._onFunctionDone = function (promise) {
if (self._pauseUntil > now && (self._pauseUntil - now > interval)) { if (self._pauseUntil > now && (self._pauseUntil - now > interval)) {
interval = self._pauseUntil - now; interval = self._pauseUntil - now;
} }
// We don't wait for this because it resolves the passed promise, not this one
Promise.delay(interval) Promise.delay(interval)
.then(function () { .then(function () {
self._log("Running new function (" self._log("Running new function ("
@ -149,17 +149,17 @@ ConcurrentCaller.prototype._onFunctionDone = function (promise) {
+ self._queue.length + " queued)"); + self._queue.length + " queued)");
self._numRunning++; self._numRunning++;
var p = self._onFunctionDone(f.func()); var result = self._onFunctionDone(f.func());
f.deferred.resolve(p); f.deferred.resolve(result);
}); });
} }
return promise; return result;
}) })
.catch(function (e) { .catch(function (e) {
self._numRunning--; self._numRunning--;
self._log("Done with function (" + self._numRunning + "/" + self._numConcurrent + ", " self._log("Error in function (" + self._numRunning + "/" + self._numConcurrent + ", "
+ self._queue.length + " in queue)"); + self._queue.length + " in queue)");
if (self.onError) { if (self.onError) {