Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit ea9aae2

Browse files
committed
Support alternate close error on Queue
1 parent d55e6f8 commit ea9aae2

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

queue.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ function Queue() {
1717
get: function () {
1818
var result = ends.promise.get("head");
1919
ends.promise = ends.promise.get("tail");
20-
return result.fail(function (reason) {
21-
closed.resolve();
22-
throw reason;
20+
return result.fail(function (error) {
21+
closed.resolve(error);
22+
throw error;
2323
});
2424
},
2525
closed: closed.promise,
26-
close: function (reason) {
27-
reason = reason || new Error("Can't get value from closed queue");
28-
var end = {head: Q.reject(reason)};
26+
close: function (error) {
27+
error = error || new Error("Can't get value from closed queue");
28+
var end = {head: Q.reject(error)};
2929
end.tail = end;
3030
ends.resolve(end);
3131
return closed.promise;

spec/queue-spec.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
var Q = require("../q");
33
var Queue = require("../queue");
4+
5+
global.Q = Q;
46
require("./lib/jasmine-promise");
57

68
describe("queue", function () {
@@ -142,7 +144,36 @@ describe("queue", function () {
142144
})
143145
.catch(function (error) {
144146
expect(error.message).toBe("Can't get value from closed queue");
145-
});
147+
})
148+
.then(function () {
149+
return queue.closed;
150+
})
151+
.then(function (error) {
152+
expect(error.message).toBe("Can't get value from closed queue");
153+
})
154+
});
155+
156+
it("should close with alternate error", function () {
157+
158+
var queue = Queue();
159+
queue.close(new Error("Alternate reason"));
160+
161+
return Q.try(function () {
162+
return queue.get();
163+
})
164+
.catch(function (error) {
165+
expect(error.message).toBe("Alternate reason");
166+
return queue.get();
167+
})
168+
.catch(function (error) {
169+
expect(error.message).toBe("Alternate reason");
170+
})
171+
.then(function () {
172+
return queue.closed;
173+
})
174+
.then(function (error) {
175+
expect(error.message).toBe("Alternate reason");
176+
})
146177
});
147178

148179
});

0 commit comments

Comments
 (0)