Skip to content

Commit

Permalink
Make Q.all emit [index, value] progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Jul 29, 2013
1 parent 12c1bb9 commit 424dbd1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
17 changes: 12 additions & 5 deletions q.js
Original file line number Diff line number Diff line change
Expand Up @@ -1505,12 +1505,19 @@ function all(promises) {
promises[index] = snapshot.value;
} else {
++countDown;
when(promise, function (value) {
promises[index] = value;
if (--countDown === 0) {
deferred.resolve(promises);
when(
promise,
function (value) {
promises[index] = value;
if (--countDown === 0) {
deferred.resolve(promises);
}
},
deferred.reject,
function (progress) {
deferred.notify([index, progress]);
}
}, deferred.reject);
);
}
}, void 0);
if (countDown === 0) {
Expand Down
36 changes: 32 additions & 4 deletions spec/q-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1085,12 +1085,11 @@ describe("propagation", function () {
});

describe("all", function () {

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

it("resolves after any constituent promise is rejected", function () {
it("rejects after any constituent promise is rejected", function () {
var toResolve = Q.defer(); // never resolve
var toReject = Q.defer();
var promises = [toResolve.promise, toReject.promise];
Expand All @@ -1115,7 +1114,7 @@ describe("all", function () {
});
});

it("resolves when passed an sparse array", function () {
it("fulfills when passed an sparse array", function () {
var toResolve = Q.defer();
var promises = [];
promises[0] = Q(0);
Expand All @@ -1138,6 +1137,35 @@ describe("all", function () {
});
});

it("sends [index, value] progress updates", function () {
var deferred1 = Q.defer();
var deferred2 = Q.defer();

var progressValues = [];

Q.delay(50).then(function () {
deferred1.notify("a");
});
Q.delay(100).then(function () {
deferred2.notify("b");
deferred2.resolve();
});
Q.delay(150).then(function () {
deferred1.notify("c");
deferred1.resolve();
});

return Q.all([deferred1.promise, deferred2.promise]).then(
function () {
expect(progressValues).toEqual([[0, "a"], [1, "b"], [0, "c"]]);
},
undefined,
function (progressValue) {
progressValues.push(progressValue);
}
)
});

});

describe("allSettled", function () {
Expand Down

0 comments on commit 424dbd1

Please sign in to comment.