-
Notifications
You must be signed in to change notification settings - Fork 1
/
promise.js
211 lines (188 loc) · 6.78 KB
/
promise.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Generated by CoffeeScript 1.6.1
(function() {
var _this = this,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice;
(function(define) {
return define(function(require, root) {
var Core, Covenant, Promise, extend, _ref, _ref1,
_this = this;
_ref1 = (_ref = require('./covenant')) != null ? _ref : root, Covenant = _ref1.Covenant, Core = _ref1.Core;
extend = function(consumer, provider) {
var k, v;
for (k in provider) {
if (!__hasProp.call(provider, k)) continue;
v = provider[k];
consumer[k] = v;
}
return consumer;
};
Promise = (function(_super) {
__extends(Promise, _super);
function Promise(resolver) {
var _this = this;
this._httpResolver = function(res) {
return Promise.prototype._httpResolver.apply(_this, arguments);
};
this._nodeResolver = function(err, value) {
return Promise.prototype._nodeResolver.apply(_this, arguments);
};
if (!(this instanceof Covenant)) {
return new Promise(resolver);
}
Promise.__super__.constructor.call(this, resolver);
extend(this.promise, {
done: this.done,
fail: this.fail,
always: this.always
});
}
Promise.pending = function() {
return new Promise;
};
Promise.fulfilled = function(value) {
return new Promise(function(resolve) {
return resolve(value);
});
};
Promise.rejected = function(reason) {
return new Promise(function(__, reject) {
return reject(reason);
});
};
Promise.fromNode = function(f) {
return function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return new Promise(function() {
return f.apply(null, __slice.call(args).concat([this._nodeResolver]));
});
};
};
Promise.delay = function(ms) {
return new Promise(function() {
var _this = this;
setTimeout((function() {
return _this.resolve(ms);
}), ms);
return this.always(function() {
return clearTimeout(t);
});
});
};
Promise.timeout = function(ms, p) {
return new Promise(function(resolve, reject) {
var err, t;
err = new Error("timeout after " + ms + " milliseconds");
t = setTimeout((function() {
return reject(err);
}), ms);
resolve(p);
return this.always(function() {
return clearTimeout(t);
});
});
};
Promise.of = function(a) {
if (a instanceof Covenant) {
return a;
} else {
return new Promise(function(res) {
return res(a);
});
}
};
Promise.map = function(promises, f) {
return new Promise(function(resolve, reject, pAll) {
var i, p, _i, _len, _results,
_this = this;
pAll.results = [];
pAll.numLeft = promises.length;
if (promises.length === 0) {
return resolve([]);
} else {
_results = [];
for (i = _i = 0, _len = promises.length; _i < _len; i = ++_i) {
p = promises[i];
_results.push((function(p, i) {
return Promise.of(p).then(f).then(function(value) {
pAll.results[i] = value;
if (--pAll.numLeft === 0) {
return resolve(pAll.results);
}
}, function(reason) {
return reject(reason);
});
})(p, i));
}
return _results;
}
});
};
Promise.all = function() {
var promises;
promises = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return Promise.map(promises, function(x) {
return x;
});
};
Promise.reduce = function(promises, f, initialValue) {
return new Promise(function(resolve, reject) {
return Promise.of(promises).then(function(array) {
var next, result;
if (array.length > 0 || (initialValue != null)) {
result = Promise.of(initialValue != null ? initialValue : array.shift());
while (next = array.shift()) {
result = (function(result, next) {
return result.then(function(acc) {
return Promise.of(next).then(function(val) {
return f(acc, val);
});
});
})(result, next);
}
return resolve(result);
} else {
return reject(new TypeError("resolve on empty array without an initial value"));
}
});
});
};
Promise.inject = Promise.reduce;
Promise.prototype.done = function(onFulfill) {
return this.then(onFulfill);
};
Promise.prototype.fail = function(onReject) {
return this.then(null, onReject);
};
Promise.prototype.always = function(callback) {
return this.then(callback, callback);
};
Promise._isPromise = function(p) {
return typeof (p != null ? p.then : void 0) === 'function';
};
Promise.prototype._nodeResolver = function(err, value) {
if (err) {
return this.reject(err);
} else {
return this.fulfill(value);
}
};
Promise.prototype._httpResolver = function(res) {
if (res.statusCode === 201) {
return res.pipe(this.stream());
} else {
return this.reject(new Error("HTTP status code " + res.statusCode));
}
};
return Promise;
}).call(this, Core);
return root.Promise = Promise;
});
})(typeof define === "function" ? define : typeof window !== "undefined" && window !== null ? function(factory) {
return factory((function() {}), window['Covenant'] || (window['Covenant'] = {}));
} : function(factory) {
return factory(require, exports, module);
});
}).call(this);