Skip to content

Commit

Permalink
Fix Promise.map unhandled rejections (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 20, 2017
1 parent 4163e82 commit 239664f
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ function MappingPromiseArray(promises, fn, limit, _filter) {
this._limit = limit;
this._inFlight = 0;
this._queue = [];
async.invoke(this._asyncInit, this, undefined);
this._asyncQueue = [];
this._init$(undefined, RESOLVE_ARRAY);
if (!this._isResolved() && this._asyncQueue.length > 0) {
async.invoke(this._drainAsyncQueue, this, this._asyncQueue);
}
this._asyncQueue = undefined;
}
util.inherits(MappingPromiseArray, PromiseArray);

MappingPromiseArray.prototype._asyncInit = function() {
this._init$(undefined, RESOLVE_ARRAY);
};

// The following hack is required because the super constructor
// might call promiseFulfilled before this.callback = fn is set
//
Expand Down Expand Up @@ -60,6 +61,11 @@ MappingPromiseArray.prototype._promiseFulfilled = function (value, index) {
if (this._isResolved()) return true;
}
} else {
if (this._asyncQueue !== undefined) {
this._asyncQueue.push([value, index]);
return false;
}

if (limit >= 1 && this._inFlight >= limit) {
values[index] = value;
this._queue.push(index);
Expand Down Expand Up @@ -144,6 +150,15 @@ MappingPromiseArray.prototype._filter = function (booleans, values) {
this._resolve(ret);
};

MappingPromiseArray.prototype._drainAsyncQueue = function (queue) {
var len = queue.length;
var item;
for (var i = 0; i < len && !this._isResolved(); ++i) {
item = queue[i];
this._promiseFulfilled(item[0], item[1]);
}
};

MappingPromiseArray.prototype.preservedValues = function () {
return this._preservedValues;
};
Expand Down

0 comments on commit 239664f

Please sign in to comment.