Commits

masklinn committed 09b90f3

Basic handling of adding a bunch of promises to the queue

  • Participants
  • Parent commits 3cc7096

Comments (0)

Files changed (2)

src/jquery.deferred-queue.js

     $.extend($.Deferred, {
         queue: function () {
             var queueDeferred = $.Deferred();
+            var promises = 0;
+
+            function resolve() {
+                if (--promises > 0) {
+                    return;
+                }
+                queueDeferred.resolve();
+            }
+
             $.extend(queueDeferred, {
-                push: function () {}
+                push: function (promise) {
+                    promises += 1;
+                    promise.then(
+                        resolve, $.proxy(this, 'reject'));
+                    return this;
+                }
             });
             return queueDeferred;
         }
     });
-})(jQuery)
+})(jQuery)
         ok(queue.done);
         ok(queue.fail);
     });
-});
+    asyncTest("Adding a deferred to the queue", 1, function () {
+        var resolved = false;
+        var queue = $.Deferred.queue();
+
+        var d = $.Deferred();
+        queue.push(d.promise()).then(function () {
+            resolved = true;
+        });
+        d.resolve();
+        setTimeout(function () {
+            ok(resolved);
+            start();
+        });
+    });
+    asyncTest("Rejecting a deferred in the queue", 2, function () {
+        var resolved = false, rejected = false;
+        var queue = $.Deferred.queue();
+
+        var d = $.Deferred();
+        queue.push(d.promise()).then(function () {
+            resolved = true;
+        }, function () {
+            rejected = true;
+        });
+        d.reject();
+        setTimeout(function () {
+            ok(rejected);
+            ok(!resolved);
+            start();
+        });
+    });
+    asyncTest("Adding more than one deferred", 1, function () {
+        var resolved = false;
+        var queue = $.Deferred.queue();
+
+        var d1 = $.Deferred(),
+            d2 = $.Deferred(),
+            d3 = $.Deferred();
+        queue.push(d1.promise())
+            .push(d2.promise())
+            .push(d3.promise())
+            .then(function () { resolved = true; });
+        d1.resolve();
+        d2.resolve();
+        d3.resolve();
+        setTimeout(function () {
+            ok(resolved);
+            start();
+        });
+    });
+    asyncTest("Rejecting one of several deferreds", 2, function () {
+        var resolved = false, rejected = false;
+        var queue = $.Deferred.queue();
+
+        var d1 = $.Deferred(),
+            d2 = $.Deferred(),
+            d3 = $.Deferred();
+        queue.push(d1.promise())
+            .push(d2.promise())
+            .push(d3.promise())
+            .then(function () { resolved = true; },
+                  function () { rejected = true; });
+        d1.resolve();
+        d2.reject();
+        d3.resolve();
+        setTimeout(function () {
+            ok(rejected);
+            ok(!resolved);
+            start();
+        });
+    });
+});