Promise.js
1.04 KB
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
38
39
40
var Class = require('./Class'),
invokeWith = require('./invokeWith'),
slice = require('./slice'),
each = require('./each'),
bind = require('./bind')
module.exports = Class(function() {
this.init = function(callback) {
this._dependants = []
this._fulfillment = null
if (callback) { this.add(callback) }
}
this.add = function(callback) {
if (this._fulfillment) { callback.apply(this, this._fulfillment) }
else { this._dependants.push(callback) }
return this
}
this.fulfill = function(/* arg1, arg2, ...*/) {
if (this._fulfillment) { throw new Error('Promise fulfilled twice') }
this._fulfillment = slice(arguments)
each(this._dependants, invokeWith.apply(this, this._fulfillment))
delete this._dependants
return this
}
this.nextTickAdd = function(callback) {
setTimeout(bind(this, this.add, callback), 0)
}
this.getCallback = function() {
return this._callback || (this._callback = bind(this, this.handle))
}
this.handle = function(err, result) {
if (err) { this.fail(err) }
else { this.fulfill(result) }
}
})