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

Add check for array parameter to Q.all() #667

Open
wants to merge 7 commits into
base: v1
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,10 @@ Promise.prototype.keys = function () {
Q.all = all;
function all(promises) {
return when(promises, function (promises) {
if (typeof arguments[0].length === "undefined") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this as opposed to Array.isArray(promises)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With Array.isArray, if all() gets passed the arguments object it would be rejected.

throw TypeError("all() must be passed an array of promises.");
}

var pendingCount = 0;
var deferred = defer();
array_reduce(promises, function (undefined, promise, index) {
Expand Down
14 changes: 14 additions & 0 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,20 @@ describe("propagation", function () {
});

describe("all", function () {
it("rejects if not passed an array", function() {
var promise = Q.defer(),
willBeRejected = Q.all(promise);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also check the case with many arguments to all()


return willBeRejected.then(
function () {
expect(true).toBe(false);
},
function (err) {
expect(err.constructor).toBe(TypeError);
}
);
});

it("fulfills when passed an empty array", function () {
return Q.all([]);
});
Expand Down