Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
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
2 changes: 1 addition & 1 deletion src/utils/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ define(function (require, exports, module) {
if (this._queue.length) {
var op = this._queue.shift();
this._curPromise = op();
this._curPromise.done(function () {
this._curPromise.always(function () {
self._curPromise = null;
self._doNext();
});
Expand Down
63 changes: 61 additions & 2 deletions test/spec/Async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,14 @@ define(function (require, exports, module) {
calledFns = {};
});

function makeFn(id, resolveNow) {
function makeFn(id, resolveNow, rejectNow) {
var result = new $.Deferred();
return {
fn: function () {
calledFns[id] = true;
if (resolveNow) {
if (rejectNow) {
result.reject();
} else if (resolveNow) {
result.resolve();
}
return result.promise();
Expand Down Expand Up @@ -440,6 +442,63 @@ define(function (require, exports, module) {
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});

it("should execute the second function if the first function is rejected", function () {
var fnInfo1 = makeFn("one"),
fnInfo2 = makeFn("two");

queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);

queue.add(fnInfo2.fn);
expect(calledFns.two).toBeUndefined();

fnInfo1.deferred.reject();
expect(calledFns.two).toBe(true);

fnInfo2.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});

it("should execute the third function after first and second functions are rejected", function () {
var fnInfo1 = makeFn("one"),
fnInfo2 = makeFn("two"),
fnInfo3 = makeFn("three");

queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);

queue.add(fnInfo2.fn);
queue.add(fnInfo3.fn);
expect(calledFns.two).toBeUndefined();
expect(calledFns.three).toBeUndefined();

fnInfo1.deferred.reject();
expect(calledFns.two).toBe(true);

fnInfo2.deferred.reject();
expect(calledFns.three).toBe(true);

fnInfo3.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});

it("should execute the second function after the already-rejected first function is added to the queue", function () {
var fnInfo1 = makeFn("one", false, true),
fnInfo2 = makeFn("two");

queue.add(fnInfo1.fn);
expect(calledFns.one).toBe(true);

queue.add(fnInfo2.fn);
expect(calledFns.two).toBe(true);

fnInfo2.deferred.resolve();
expect(queue._queue.length).toBe(0);
expect(queue._curPromise).toBe(null);
});
});
});
});