Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Promise.map unhandled rejections (#1487) #1489

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 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,12 @@ MappingPromiseArray.prototype._promiseFulfilled = function (value, index) {
if (this._isResolved()) return true;
}
} else {
if (this._asyncQueue !== undefined) {
values[index] = value;
this._asyncQueue.push(index);
return false;
}

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

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

MappingPromiseArray.prototype.preservedValues = function () {
return this._preservedValues;
};
Expand Down
18 changes: 14 additions & 4 deletions test/mocha/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ describe("Promise.map-test", function () {
});

specify("should reject when input contains rejection", function() {
var input = [Promise.resolve(1), Promise.reject(2), Promise.resolve(3)];
return Promise.map(input, mapper).then(
var input = [Promise.resolve(1), Promise.reject(2), Promise.reject(3)];
var promise = Promise.map(input, mapper);

assert(!input[1]._isRejectionUnhandled());
assert(!input[2]._isRejectionUnhandled());

return promise.then(
assert.fail,
function(result) {
assert(result === 2);
Expand Down Expand Up @@ -232,8 +237,13 @@ describe("Promise.map-test with concurrency", function () {
});

specify("should reject when input contains rejection with concurrency", function() {
var input = [Promise.resolve(1), Promise.reject(2), Promise.resolve(3)];
return Promise.map(input, mapper, concurrency).then(
var input = [Promise.resolve(1), Promise.reject(2), Promise.reject(3)];
var promise = Promise.map(input, mapper, concurrency);

assert(!input[1]._isRejectionUnhandled());
assert(!input[2]._isRejectionUnhandled());

return promise.then(
assert.fail,
function(result) {
assert(result === 2);
Expand Down