forked from kriskowal/q
-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue.js
35 lines (32 loc) · 942 Bytes
/
queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var Q = require("./q");
module.exports = Queue;
function Queue() {
var ends = Q.defer();
var closed = Q.defer();
return {
put: function (value) {
var next = Q.defer();
ends.resolve({
head: value,
tail: next.promise
});
ends.resolve = next.resolve;
},
get: function () {
var result = ends.promise.get("head");
ends.promise = ends.promise.get("tail");
return result.fail(function (reason) {
closed.resolve();
throw reason;
});
},
closed: closed.promise,
close: function (reason) {
reason = reason || new Error("Can't get value from closed queue");
var end = {head: Q.reject(reason)};
end.tail = end;
ends.resolve(end);
return closed.promise;
}
};
}