Call next function in finally block in ConcurrentCaller

This commit is contained in:
Dan Stillman 2015-05-05 02:58:22 -04:00
parent bb760707d4
commit 574f636683

View File

@ -123,6 +123,7 @@ ConcurrentCaller.prototype.stop = function () {
ConcurrentCaller.prototype._onFunctionDone = function (promise) {
var self = this;
return promise.then(function (result) {
self._numRunning--;
@ -130,30 +131,6 @@ ConcurrentCaller.prototype._onFunctionDone = function (promise) {
+ self._numRunning + "/" + self._numConcurrent + " running, "
+ self._queue.length + " queued)");
// If there's a function to call and we're under the concurrent limit,
// run it now
let f = self._queue.shift();
if (f && self._numRunning < self._numConcurrent) {
// Wait until the specified interval has elapsed or the current
// pause (if there is one) is over, whichever is longer
let interval = self._interval;
let now = Date.now();
if (self._pauseUntil > now && (self._pauseUntil - now > interval)) {
interval = self._pauseUntil - now;
}
// We don't wait for this because it resolves the passed promise, not this one
Promise.delay(interval)
.then(function () {
self._log("Running new function ("
+ self._numRunning + "/" + self._numConcurrent + " running, "
+ self._queue.length + " queued)");
self._numRunning++;
var result = self._onFunctionDone(f.func());
f.deferred.resolve(result);
});
}
return result;
})
.catch(function (e) {
@ -172,6 +149,28 @@ ConcurrentCaller.prototype._onFunctionDone = function (promise) {
}
throw e;
})
.finally(function () {
// If there's a function to call and we're under the concurrent limit, run it now
var f = self._queue.shift();
if (f && self._numRunning < self._numConcurrent) {
// Wait until the specified interval has elapsed or the current
// pause (if there is one) is over, whichever is longer
let interval = self._interval;
let now = Date.now();
if (self._pauseUntil > now && (self._pauseUntil - now > interval)) {
interval = self._pauseUntil - now;
}
Promise.delay(interval)
.then(function () {
self._log("Running new function ("
+ self._numRunning + "/" + self._numConcurrent + " running, "
+ self._queue.length + " queued)");
self._numRunning++;
f.deferred.resolve(self._onFunctionDone(f.func()));
});
}
});
};