From 424dbd1f8c4a6a40a4b2c341598955b3880bfe79 Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Mon, 29 Jul 2013 00:11:12 -0400 Subject: [PATCH] Make `Q.all` emit [index, value] progress. Closes #333. --- q.js | 17 ++++++++++++----- spec/q-spec.js | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/q.js b/q.js index f7475c53..a9ba71b8 100644 --- a/q.js +++ b/q.js @@ -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) { diff --git a/spec/q-spec.js b/spec/q-spec.js index 6e0dc62e..77f523c2 100644 --- a/spec/q-spec.js +++ b/spec/q-spec.js @@ -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]; @@ -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); @@ -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 () {