pledge.js
779 Bytes
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
35
36
37
'use strict';
var RingBuffer = require('./ring_buffer');
var Pledge = function() {
this._complete = false;
this._callbacks = new RingBuffer(Pledge.QUEUE_SIZE);
};
Pledge.QUEUE_SIZE = 4;
Pledge.all = function(list) {
var pledge = new Pledge(),
pending = list.length,
n = pending;
if (pending === 0) pledge.done();
while (n--) list[n].then(function() {
pending -= 1;
if (pending === 0) pledge.done();
});
return pledge;
};
Pledge.prototype.then = function(callback) {
if (this._complete) callback();
else this._callbacks.push(callback);
};
Pledge.prototype.done = function() {
this._complete = true;
var callbacks = this._callbacks, callback;
while (callback = callbacks.shift()) callback();
};
module.exports = Pledge;