Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 6cdd1de

Browse files
author
Narciso Jaramillo
committed
Merge pull request #7407 from jayther/promisequeuerejectfix
Fixed bug in utils/Async.PromiseQueue where a rejected promise keeps the...
2 parents 1e8ec31 + df1b47c commit 6cdd1de

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

src/utils/Async.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ define(function (require, exports, module) {
487487
if (this._queue.length) {
488488
var op = this._queue.shift();
489489
this._curPromise = op();
490-
this._curPromise.done(function () {
490+
this._curPromise.always(function () {
491491
self._curPromise = null;
492492
self._doNext();
493493
});

test/spec/Async-test.js

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,14 @@ define(function (require, exports, module) {
314314
calledFns = {};
315315
});
316316

317-
function makeFn(id, resolveNow) {
317+
function makeFn(id, resolveNow, rejectNow) {
318318
var result = new $.Deferred();
319319
return {
320320
fn: function () {
321321
calledFns[id] = true;
322-
if (resolveNow) {
322+
if (rejectNow) {
323+
result.reject();
324+
} else if (resolveNow) {
323325
result.resolve();
324326
}
325327
return result.promise();
@@ -440,6 +442,63 @@ define(function (require, exports, module) {
440442
expect(queue._queue.length).toBe(0);
441443
expect(queue._curPromise).toBe(null);
442444
});
445+
446+
it("should execute the second function if the first function is rejected", function () {
447+
var fnInfo1 = makeFn("one"),
448+
fnInfo2 = makeFn("two");
449+
450+
queue.add(fnInfo1.fn);
451+
expect(calledFns.one).toBe(true);
452+
453+
queue.add(fnInfo2.fn);
454+
expect(calledFns.two).toBeUndefined();
455+
456+
fnInfo1.deferred.reject();
457+
expect(calledFns.two).toBe(true);
458+
459+
fnInfo2.deferred.resolve();
460+
expect(queue._queue.length).toBe(0);
461+
expect(queue._curPromise).toBe(null);
462+
});
463+
464+
it("should execute the third function after first and second functions are rejected", function () {
465+
var fnInfo1 = makeFn("one"),
466+
fnInfo2 = makeFn("two"),
467+
fnInfo3 = makeFn("three");
468+
469+
queue.add(fnInfo1.fn);
470+
expect(calledFns.one).toBe(true);
471+
472+
queue.add(fnInfo2.fn);
473+
queue.add(fnInfo3.fn);
474+
expect(calledFns.two).toBeUndefined();
475+
expect(calledFns.three).toBeUndefined();
476+
477+
fnInfo1.deferred.reject();
478+
expect(calledFns.two).toBe(true);
479+
480+
fnInfo2.deferred.reject();
481+
expect(calledFns.three).toBe(true);
482+
483+
fnInfo3.deferred.resolve();
484+
expect(queue._queue.length).toBe(0);
485+
expect(queue._curPromise).toBe(null);
486+
});
487+
488+
it("should execute the second function after the already-rejected first function is added to the queue", function () {
489+
var fnInfo1 = makeFn("one", false, true),
490+
fnInfo2 = makeFn("two");
491+
492+
queue.add(fnInfo1.fn);
493+
expect(calledFns.one).toBe(true);
494+
495+
queue.add(fnInfo2.fn);
496+
expect(calledFns.two).toBe(true);
497+
498+
fnInfo2.deferred.resolve();
499+
expect(queue._queue.length).toBe(0);
500+
expect(queue._curPromise).toBe(null);
501+
});
443502
});
444503
});
445504
});

0 commit comments

Comments
 (0)