-
Notifications
You must be signed in to change notification settings - Fork 127
/
promise.js
218 lines (186 loc) · 5.45 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
212
213
214
215
216
217
218
/*
* Copyright 2012-2013 (c) Pierre Duquesne <stackp@online.fr>
* Licensed under the New BSD License.
* https://github.com/stackp/promisejs
*/
(function(exports) {
function Promise() {
this._callbacks = [];
}
Promise.prototype.then = function(func, context) {
var p;
if (this._isdone) {
p = func.apply(context, this.result);
} else {
p = new Promise();
this._callbacks.push(function () {
var res = func.apply(context, arguments);
if (res && typeof res.then === 'function')
res.then(p.done, p);
});
}
return p;
};
Promise.prototype.done = function() {
this.result = arguments;
this._isdone = true;
for (var i = 0; i < this._callbacks.length; i++) {
this._callbacks[i].apply(null, arguments);
}
this._callbacks = [];
};
function join(promises) {
var p = new Promise();
var results = [];
if (!promises || !promises.length) {
p.done(results);
return p;
}
var numdone = 0;
var total = promises.length;
function notifier(i) {
return function() {
numdone += 1;
results[i] = Array.prototype.slice.call(arguments);
if (numdone === total) {
p.done(results);
}
};
}
for (var i = 0; i < total; i++) {
promises[i].then(notifier(i));
}
return p;
}
function chain(funcs, args) {
var p = new Promise();
if (funcs.length === 0) {
p.done.apply(p, args);
} else {
funcs[0].apply(null, args).then(function() {
funcs.splice(0, 1);
chain(funcs, arguments).then(function() {
p.done.apply(p, arguments);
});
});
}
return p;
}
/*
* AJAX requests
*/
function _encode(data) {
var payload = "";
if (typeof data === "string") {
payload = data;
} else {
var e = encodeURIComponent;
var params = [];
for (var k in data) {
if (data.hasOwnProperty(k)) {
params.push(e(k) + '=' + e(data[k]));
}
}
payload = params.join('&')
}
return payload;
}
function new_xhr() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xhr;
}
function ajax(method, url, data, headers) {
var p = new Promise();
var xhr, payload;
data = data || {};
headers = headers || {};
try {
xhr = new_xhr();
} catch (e) {
p.done(promise.ENOXHR, "");
return p;
}
payload = _encode(data);
if (method === 'GET' && payload) {
url += '?' + payload;
payload = null;
}
xhr.open(method, url);
var content_type = 'application/x-www-form-urlencoded';
for (var h in headers) {
if (headers.hasOwnProperty(h)) {
if (h.toLowerCase() === 'content-type')
content_type = headers[h];
else
xhr.setRequestHeader(h, headers[h]);
}
}
xhr.setRequestHeader('Content-type', content_type);
function onTimeout() {
xhr.abort();
p.done(promise.ETIMEOUT, "", xhr);
}
var timeout = promise.ajaxTimeout;
if (timeout) {
var tid = setTimeout(onTimeout, timeout);
}
xhr.onreadystatechange = function() {
if (timeout) {
clearTimeout(tid);
}
if (xhr.readyState === 4) {
var err = (!xhr.status ||
(xhr.status < 200 || xhr.status >= 300) &&
xhr.status !== 304);
p.done(err, xhr.responseText, xhr);
}
};
xhr.send(payload);
return p;
}
function _ajaxer(method) {
return function(url, data, headers) {
return ajax(method, url, data, headers);
};
}
var promise = {
Promise: Promise,
join: join,
chain: chain,
ajax: ajax,
get: _ajaxer('GET'),
post: _ajaxer('POST'),
put: _ajaxer('PUT'),
del: _ajaxer('DELETE'),
/* Error codes */
ENOXHR: 1,
ETIMEOUT: 2,
/**
* Configuration parameter: time in milliseconds after which a
* pending AJAX request is considered unresponsive and is
* aborted. Useful to deal with bad connectivity (e.g. on a
* mobile network). A 0 value disables AJAX timeouts.
*
* Aborted requests resolve the promise with a ETIMEOUT error
* code.
*/
ajaxTimeout: 0
};
if (typeof define === 'function' && define.amd) {
/* AMD support */
define(function() {
return promise;
});
} else {
exports.promise = promise;
}
})(this);