es6-promise-rails 1.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +15 -15
- data/README.md +1 -1
- data/lib/es6/promise/rails/version.rb +1 -1
- data/vendor/assets/javascripts/es6-promise.js +966 -0
- metadata +9 -9
- data/vendor/assets/javascripts/promise.js +0 -690
@@ -0,0 +1,966 @@
|
|
1
|
+
/*!
|
2
|
+
* @overview es6-promise - a tiny implementation of Promises/A+.
|
3
|
+
* @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
|
4
|
+
* @license Licensed under MIT license
|
5
|
+
* See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE
|
6
|
+
* @version 2.0.0
|
7
|
+
*/
|
8
|
+
|
9
|
+
(function() {
|
10
|
+
"use strict";
|
11
|
+
|
12
|
+
function $$utils$$objectOrFunction(x) {
|
13
|
+
return typeof x === 'function' || (typeof x === 'object' && x !== null);
|
14
|
+
}
|
15
|
+
|
16
|
+
function $$utils$$isFunction(x) {
|
17
|
+
return typeof x === 'function';
|
18
|
+
}
|
19
|
+
|
20
|
+
function $$utils$$isMaybeThenable(x) {
|
21
|
+
return typeof x === 'object' && x !== null;
|
22
|
+
}
|
23
|
+
|
24
|
+
var $$utils$$_isArray;
|
25
|
+
|
26
|
+
if (!Array.isArray) {
|
27
|
+
$$utils$$_isArray = function (x) {
|
28
|
+
return Object.prototype.toString.call(x) === '[object Array]';
|
29
|
+
};
|
30
|
+
} else {
|
31
|
+
$$utils$$_isArray = Array.isArray;
|
32
|
+
}
|
33
|
+
|
34
|
+
var $$utils$$isArray = $$utils$$_isArray;
|
35
|
+
var $$utils$$now = Date.now || function() { return new Date().getTime(); };
|
36
|
+
function $$utils$$F() { }
|
37
|
+
|
38
|
+
var $$utils$$o_create = (Object.create || function (o) {
|
39
|
+
if (arguments.length > 1) {
|
40
|
+
throw new Error('Second argument not supported');
|
41
|
+
}
|
42
|
+
if (typeof o !== 'object') {
|
43
|
+
throw new TypeError('Argument must be an object');
|
44
|
+
}
|
45
|
+
$$utils$$F.prototype = o;
|
46
|
+
return new $$utils$$F();
|
47
|
+
});
|
48
|
+
|
49
|
+
var $$asap$$len = 0;
|
50
|
+
|
51
|
+
var $$asap$$default = function asap(callback, arg) {
|
52
|
+
$$asap$$queue[$$asap$$len] = callback;
|
53
|
+
$$asap$$queue[$$asap$$len + 1] = arg;
|
54
|
+
$$asap$$len += 2;
|
55
|
+
if ($$asap$$len === 2) {
|
56
|
+
// If len is 1, that means that we need to schedule an async flush.
|
57
|
+
// If additional callbacks are queued before the queue is flushed, they
|
58
|
+
// will be processed by this flush that we are scheduling.
|
59
|
+
$$asap$$scheduleFlush();
|
60
|
+
}
|
61
|
+
};
|
62
|
+
|
63
|
+
var $$asap$$browserGlobal = (typeof window !== 'undefined') ? window : {};
|
64
|
+
var $$asap$$BrowserMutationObserver = $$asap$$browserGlobal.MutationObserver || $$asap$$browserGlobal.WebKitMutationObserver;
|
65
|
+
|
66
|
+
// test for web worker but not in IE10
|
67
|
+
var $$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' &&
|
68
|
+
typeof importScripts !== 'undefined' &&
|
69
|
+
typeof MessageChannel !== 'undefined';
|
70
|
+
|
71
|
+
// node
|
72
|
+
function $$asap$$useNextTick() {
|
73
|
+
return function() {
|
74
|
+
process.nextTick($$asap$$flush);
|
75
|
+
};
|
76
|
+
}
|
77
|
+
|
78
|
+
function $$asap$$useMutationObserver() {
|
79
|
+
var iterations = 0;
|
80
|
+
var observer = new $$asap$$BrowserMutationObserver($$asap$$flush);
|
81
|
+
var node = document.createTextNode('');
|
82
|
+
observer.observe(node, { characterData: true });
|
83
|
+
|
84
|
+
return function() {
|
85
|
+
node.data = (iterations = ++iterations % 2);
|
86
|
+
};
|
87
|
+
}
|
88
|
+
|
89
|
+
// web worker
|
90
|
+
function $$asap$$useMessageChannel() {
|
91
|
+
var channel = new MessageChannel();
|
92
|
+
channel.port1.onmessage = $$asap$$flush;
|
93
|
+
return function () {
|
94
|
+
channel.port2.postMessage(0);
|
95
|
+
};
|
96
|
+
}
|
97
|
+
|
98
|
+
function $$asap$$useSetTimeout() {
|
99
|
+
return function() {
|
100
|
+
setTimeout($$asap$$flush, 1);
|
101
|
+
};
|
102
|
+
}
|
103
|
+
|
104
|
+
var $$asap$$queue = new Array(1000);
|
105
|
+
|
106
|
+
function $$asap$$flush() {
|
107
|
+
for (var i = 0; i < $$asap$$len; i+=2) {
|
108
|
+
var callback = $$asap$$queue[i];
|
109
|
+
var arg = $$asap$$queue[i+1];
|
110
|
+
|
111
|
+
callback(arg);
|
112
|
+
|
113
|
+
$$asap$$queue[i] = undefined;
|
114
|
+
$$asap$$queue[i+1] = undefined;
|
115
|
+
}
|
116
|
+
|
117
|
+
$$asap$$len = 0;
|
118
|
+
}
|
119
|
+
|
120
|
+
var $$asap$$scheduleFlush;
|
121
|
+
|
122
|
+
// Decide what async method to use to triggering processing of queued callbacks:
|
123
|
+
if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
|
124
|
+
$$asap$$scheduleFlush = $$asap$$useNextTick();
|
125
|
+
} else if ($$asap$$BrowserMutationObserver) {
|
126
|
+
$$asap$$scheduleFlush = $$asap$$useMutationObserver();
|
127
|
+
} else if ($$asap$$isWorker) {
|
128
|
+
$$asap$$scheduleFlush = $$asap$$useMessageChannel();
|
129
|
+
} else {
|
130
|
+
$$asap$$scheduleFlush = $$asap$$useSetTimeout();
|
131
|
+
}
|
132
|
+
|
133
|
+
function $$$internal$$noop() {}
|
134
|
+
var $$$internal$$PENDING = void 0;
|
135
|
+
var $$$internal$$FULFILLED = 1;
|
136
|
+
var $$$internal$$REJECTED = 2;
|
137
|
+
var $$$internal$$GET_THEN_ERROR = new $$$internal$$ErrorObject();
|
138
|
+
|
139
|
+
function $$$internal$$selfFullfillment() {
|
140
|
+
return new TypeError("You cannot resolve a promise with itself");
|
141
|
+
}
|
142
|
+
|
143
|
+
function $$$internal$$cannotReturnOwn() {
|
144
|
+
return new TypeError('A promises callback cannot return that same promise.')
|
145
|
+
}
|
146
|
+
|
147
|
+
function $$$internal$$getThen(promise) {
|
148
|
+
try {
|
149
|
+
return promise.then;
|
150
|
+
} catch(error) {
|
151
|
+
$$$internal$$GET_THEN_ERROR.error = error;
|
152
|
+
return $$$internal$$GET_THEN_ERROR;
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
function $$$internal$$tryThen(then, value, fulfillmentHandler, rejectionHandler) {
|
157
|
+
try {
|
158
|
+
then.call(value, fulfillmentHandler, rejectionHandler);
|
159
|
+
} catch(e) {
|
160
|
+
return e;
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
function $$$internal$$handleForeignThenable(promise, thenable, then) {
|
165
|
+
$$asap$$default(function(promise) {
|
166
|
+
var sealed = false;
|
167
|
+
var error = $$$internal$$tryThen(then, thenable, function(value) {
|
168
|
+
if (sealed) { return; }
|
169
|
+
sealed = true;
|
170
|
+
if (thenable !== value) {
|
171
|
+
$$$internal$$resolve(promise, value);
|
172
|
+
} else {
|
173
|
+
$$$internal$$fulfill(promise, value);
|
174
|
+
}
|
175
|
+
}, function(reason) {
|
176
|
+
if (sealed) { return; }
|
177
|
+
sealed = true;
|
178
|
+
|
179
|
+
$$$internal$$reject(promise, reason);
|
180
|
+
}, 'Settle: ' + (promise._label || ' unknown promise'));
|
181
|
+
|
182
|
+
if (!sealed && error) {
|
183
|
+
sealed = true;
|
184
|
+
$$$internal$$reject(promise, error);
|
185
|
+
}
|
186
|
+
}, promise);
|
187
|
+
}
|
188
|
+
|
189
|
+
function $$$internal$$handleOwnThenable(promise, thenable) {
|
190
|
+
if (thenable._state === $$$internal$$FULFILLED) {
|
191
|
+
$$$internal$$fulfill(promise, thenable._result);
|
192
|
+
} else if (promise._state === $$$internal$$REJECTED) {
|
193
|
+
$$$internal$$reject(promise, thenable._result);
|
194
|
+
} else {
|
195
|
+
$$$internal$$subscribe(thenable, undefined, function(value) {
|
196
|
+
$$$internal$$resolve(promise, value);
|
197
|
+
}, function(reason) {
|
198
|
+
$$$internal$$reject(promise, reason);
|
199
|
+
});
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
203
|
+
function $$$internal$$handleMaybeThenable(promise, maybeThenable) {
|
204
|
+
if (maybeThenable.constructor === promise.constructor) {
|
205
|
+
$$$internal$$handleOwnThenable(promise, maybeThenable);
|
206
|
+
} else {
|
207
|
+
var then = $$$internal$$getThen(maybeThenable);
|
208
|
+
|
209
|
+
if (then === $$$internal$$GET_THEN_ERROR) {
|
210
|
+
$$$internal$$reject(promise, $$$internal$$GET_THEN_ERROR.error);
|
211
|
+
} else if (then === undefined) {
|
212
|
+
$$$internal$$fulfill(promise, maybeThenable);
|
213
|
+
} else if ($$utils$$isFunction(then)) {
|
214
|
+
$$$internal$$handleForeignThenable(promise, maybeThenable, then);
|
215
|
+
} else {
|
216
|
+
$$$internal$$fulfill(promise, maybeThenable);
|
217
|
+
}
|
218
|
+
}
|
219
|
+
}
|
220
|
+
|
221
|
+
function $$$internal$$resolve(promise, value) {
|
222
|
+
if (promise === value) {
|
223
|
+
$$$internal$$reject(promise, $$$internal$$selfFullfillment());
|
224
|
+
} else if ($$utils$$objectOrFunction(value)) {
|
225
|
+
$$$internal$$handleMaybeThenable(promise, value);
|
226
|
+
} else {
|
227
|
+
$$$internal$$fulfill(promise, value);
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
function $$$internal$$publishRejection(promise) {
|
232
|
+
if (promise._onerror) {
|
233
|
+
promise._onerror(promise._result);
|
234
|
+
}
|
235
|
+
|
236
|
+
$$$internal$$publish(promise);
|
237
|
+
}
|
238
|
+
|
239
|
+
function $$$internal$$fulfill(promise, value) {
|
240
|
+
if (promise._state !== $$$internal$$PENDING) { return; }
|
241
|
+
|
242
|
+
promise._result = value;
|
243
|
+
promise._state = $$$internal$$FULFILLED;
|
244
|
+
|
245
|
+
if (promise._subscribers.length === 0) {
|
246
|
+
} else {
|
247
|
+
$$asap$$default($$$internal$$publish, promise);
|
248
|
+
}
|
249
|
+
}
|
250
|
+
|
251
|
+
function $$$internal$$reject(promise, reason) {
|
252
|
+
if (promise._state !== $$$internal$$PENDING) { return; }
|
253
|
+
promise._state = $$$internal$$REJECTED;
|
254
|
+
promise._result = reason;
|
255
|
+
|
256
|
+
$$asap$$default($$$internal$$publishRejection, promise);
|
257
|
+
}
|
258
|
+
|
259
|
+
function $$$internal$$subscribe(parent, child, onFulfillment, onRejection) {
|
260
|
+
var subscribers = parent._subscribers;
|
261
|
+
var length = subscribers.length;
|
262
|
+
|
263
|
+
parent._onerror = null;
|
264
|
+
|
265
|
+
subscribers[length] = child;
|
266
|
+
subscribers[length + $$$internal$$FULFILLED] = onFulfillment;
|
267
|
+
subscribers[length + $$$internal$$REJECTED] = onRejection;
|
268
|
+
|
269
|
+
if (length === 0 && parent._state) {
|
270
|
+
$$asap$$default($$$internal$$publish, parent);
|
271
|
+
}
|
272
|
+
}
|
273
|
+
|
274
|
+
function $$$internal$$publish(promise) {
|
275
|
+
var subscribers = promise._subscribers;
|
276
|
+
var settled = promise._state;
|
277
|
+
|
278
|
+
if (subscribers.length === 0) { return; }
|
279
|
+
|
280
|
+
var child, callback, detail = promise._result;
|
281
|
+
|
282
|
+
for (var i = 0; i < subscribers.length; i += 3) {
|
283
|
+
child = subscribers[i];
|
284
|
+
callback = subscribers[i + settled];
|
285
|
+
|
286
|
+
if (child) {
|
287
|
+
$$$internal$$invokeCallback(settled, child, callback, detail);
|
288
|
+
} else {
|
289
|
+
callback(detail);
|
290
|
+
}
|
291
|
+
}
|
292
|
+
|
293
|
+
promise._subscribers.length = 0;
|
294
|
+
}
|
295
|
+
|
296
|
+
function $$$internal$$ErrorObject() {
|
297
|
+
this.error = null;
|
298
|
+
}
|
299
|
+
|
300
|
+
var $$$internal$$TRY_CATCH_ERROR = new $$$internal$$ErrorObject();
|
301
|
+
|
302
|
+
function $$$internal$$tryCatch(callback, detail) {
|
303
|
+
try {
|
304
|
+
return callback(detail);
|
305
|
+
} catch(e) {
|
306
|
+
$$$internal$$TRY_CATCH_ERROR.error = e;
|
307
|
+
return $$$internal$$TRY_CATCH_ERROR;
|
308
|
+
}
|
309
|
+
}
|
310
|
+
|
311
|
+
function $$$internal$$invokeCallback(settled, promise, callback, detail) {
|
312
|
+
var hasCallback = $$utils$$isFunction(callback),
|
313
|
+
value, error, succeeded, failed;
|
314
|
+
|
315
|
+
if (hasCallback) {
|
316
|
+
value = $$$internal$$tryCatch(callback, detail);
|
317
|
+
|
318
|
+
if (value === $$$internal$$TRY_CATCH_ERROR) {
|
319
|
+
failed = true;
|
320
|
+
error = value.error;
|
321
|
+
value = null;
|
322
|
+
} else {
|
323
|
+
succeeded = true;
|
324
|
+
}
|
325
|
+
|
326
|
+
if (promise === value) {
|
327
|
+
$$$internal$$reject(promise, $$$internal$$cannotReturnOwn());
|
328
|
+
return;
|
329
|
+
}
|
330
|
+
|
331
|
+
} else {
|
332
|
+
value = detail;
|
333
|
+
succeeded = true;
|
334
|
+
}
|
335
|
+
|
336
|
+
if (promise._state !== $$$internal$$PENDING) {
|
337
|
+
// noop
|
338
|
+
} else if (hasCallback && succeeded) {
|
339
|
+
$$$internal$$resolve(promise, value);
|
340
|
+
} else if (failed) {
|
341
|
+
$$$internal$$reject(promise, error);
|
342
|
+
} else if (settled === $$$internal$$FULFILLED) {
|
343
|
+
$$$internal$$fulfill(promise, value);
|
344
|
+
} else if (settled === $$$internal$$REJECTED) {
|
345
|
+
$$$internal$$reject(promise, value);
|
346
|
+
}
|
347
|
+
}
|
348
|
+
|
349
|
+
function $$$internal$$initializePromise(promise, resolver) {
|
350
|
+
try {
|
351
|
+
resolver(function resolvePromise(value){
|
352
|
+
$$$internal$$resolve(promise, value);
|
353
|
+
}, function rejectPromise(reason) {
|
354
|
+
$$$internal$$reject(promise, reason);
|
355
|
+
});
|
356
|
+
} catch(e) {
|
357
|
+
$$$internal$$reject(promise, e);
|
358
|
+
}
|
359
|
+
}
|
360
|
+
|
361
|
+
function $$$enumerator$$makeSettledResult(state, position, value) {
|
362
|
+
if (state === $$$internal$$FULFILLED) {
|
363
|
+
return {
|
364
|
+
state: 'fulfilled',
|
365
|
+
value: value
|
366
|
+
};
|
367
|
+
} else {
|
368
|
+
return {
|
369
|
+
state: 'rejected',
|
370
|
+
reason: value
|
371
|
+
};
|
372
|
+
}
|
373
|
+
}
|
374
|
+
|
375
|
+
function $$$enumerator$$Enumerator(Constructor, input, abortOnReject, label) {
|
376
|
+
this._instanceConstructor = Constructor;
|
377
|
+
this.promise = new Constructor($$$internal$$noop, label);
|
378
|
+
this._abortOnReject = abortOnReject;
|
379
|
+
|
380
|
+
if (this._validateInput(input)) {
|
381
|
+
this._input = input;
|
382
|
+
this.length = input.length;
|
383
|
+
this._remaining = input.length;
|
384
|
+
|
385
|
+
this._init();
|
386
|
+
|
387
|
+
if (this.length === 0) {
|
388
|
+
$$$internal$$fulfill(this.promise, this._result);
|
389
|
+
} else {
|
390
|
+
this.length = this.length || 0;
|
391
|
+
this._enumerate();
|
392
|
+
if (this._remaining === 0) {
|
393
|
+
$$$internal$$fulfill(this.promise, this._result);
|
394
|
+
}
|
395
|
+
}
|
396
|
+
} else {
|
397
|
+
$$$internal$$reject(this.promise, this._validationError());
|
398
|
+
}
|
399
|
+
}
|
400
|
+
|
401
|
+
$$$enumerator$$Enumerator.prototype._validateInput = function(input) {
|
402
|
+
return $$utils$$isArray(input);
|
403
|
+
};
|
404
|
+
|
405
|
+
$$$enumerator$$Enumerator.prototype._validationError = function() {
|
406
|
+
return new Error('Array Methods must be provided an Array');
|
407
|
+
};
|
408
|
+
|
409
|
+
$$$enumerator$$Enumerator.prototype._init = function() {
|
410
|
+
this._result = new Array(this.length);
|
411
|
+
};
|
412
|
+
|
413
|
+
var $$$enumerator$$default = $$$enumerator$$Enumerator;
|
414
|
+
|
415
|
+
$$$enumerator$$Enumerator.prototype._enumerate = function() {
|
416
|
+
var length = this.length;
|
417
|
+
var promise = this.promise;
|
418
|
+
var input = this._input;
|
419
|
+
|
420
|
+
for (var i = 0; promise._state === $$$internal$$PENDING && i < length; i++) {
|
421
|
+
this._eachEntry(input[i], i);
|
422
|
+
}
|
423
|
+
};
|
424
|
+
|
425
|
+
$$$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) {
|
426
|
+
var c = this._instanceConstructor;
|
427
|
+
if ($$utils$$isMaybeThenable(entry)) {
|
428
|
+
if (entry.constructor === c && entry._state !== $$$internal$$PENDING) {
|
429
|
+
entry._onerror = null;
|
430
|
+
this._settledAt(entry._state, i, entry._result);
|
431
|
+
} else {
|
432
|
+
this._willSettleAt(c.resolve(entry), i);
|
433
|
+
}
|
434
|
+
} else {
|
435
|
+
this._remaining--;
|
436
|
+
this._result[i] = this._makeResult($$$internal$$FULFILLED, i, entry);
|
437
|
+
}
|
438
|
+
};
|
439
|
+
|
440
|
+
$$$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) {
|
441
|
+
var promise = this.promise;
|
442
|
+
|
443
|
+
if (promise._state === $$$internal$$PENDING) {
|
444
|
+
this._remaining--;
|
445
|
+
|
446
|
+
if (this._abortOnReject && state === $$$internal$$REJECTED) {
|
447
|
+
$$$internal$$reject(promise, value);
|
448
|
+
} else {
|
449
|
+
this._result[i] = this._makeResult(state, i, value);
|
450
|
+
}
|
451
|
+
}
|
452
|
+
|
453
|
+
if (this._remaining === 0) {
|
454
|
+
$$$internal$$fulfill(promise, this._result);
|
455
|
+
}
|
456
|
+
};
|
457
|
+
|
458
|
+
$$$enumerator$$Enumerator.prototype._makeResult = function(state, i, value) {
|
459
|
+
return value;
|
460
|
+
};
|
461
|
+
|
462
|
+
$$$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) {
|
463
|
+
var enumerator = this;
|
464
|
+
|
465
|
+
$$$internal$$subscribe(promise, undefined, function(value) {
|
466
|
+
enumerator._settledAt($$$internal$$FULFILLED, i, value);
|
467
|
+
}, function(reason) {
|
468
|
+
enumerator._settledAt($$$internal$$REJECTED, i, reason);
|
469
|
+
});
|
470
|
+
};
|
471
|
+
|
472
|
+
var $$promise$all$$default = function all(entries, label) {
|
473
|
+
return new $$$enumerator$$default(this, entries, true /* abort on reject */, label).promise;
|
474
|
+
};
|
475
|
+
|
476
|
+
var $$promise$race$$default = function race(entries, label) {
|
477
|
+
/*jshint validthis:true */
|
478
|
+
var Constructor = this;
|
479
|
+
|
480
|
+
var promise = new Constructor($$$internal$$noop, label);
|
481
|
+
|
482
|
+
if (!$$utils$$isArray(entries)) {
|
483
|
+
$$$internal$$reject(promise, new TypeError('You must pass an array to race.'));
|
484
|
+
return promise;
|
485
|
+
}
|
486
|
+
|
487
|
+
var length = entries.length;
|
488
|
+
|
489
|
+
function onFulfillment(value) {
|
490
|
+
$$$internal$$resolve(promise, value);
|
491
|
+
}
|
492
|
+
|
493
|
+
function onRejection(reason) {
|
494
|
+
$$$internal$$reject(promise, reason);
|
495
|
+
}
|
496
|
+
|
497
|
+
for (var i = 0; promise._state === $$$internal$$PENDING && i < length; i++) {
|
498
|
+
$$$internal$$subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection);
|
499
|
+
}
|
500
|
+
|
501
|
+
return promise;
|
502
|
+
};
|
503
|
+
|
504
|
+
var $$promise$resolve$$default = function resolve(object, label) {
|
505
|
+
/*jshint validthis:true */
|
506
|
+
var Constructor = this;
|
507
|
+
|
508
|
+
if (object && typeof object === 'object' && object.constructor === Constructor) {
|
509
|
+
return object;
|
510
|
+
}
|
511
|
+
|
512
|
+
var promise = new Constructor($$$internal$$noop, label);
|
513
|
+
$$$internal$$resolve(promise, object);
|
514
|
+
return promise;
|
515
|
+
};
|
516
|
+
|
517
|
+
var $$promise$reject$$default = function reject(reason, label) {
|
518
|
+
/*jshint validthis:true */
|
519
|
+
var Constructor = this;
|
520
|
+
var promise = new Constructor($$$internal$$noop, label);
|
521
|
+
$$$internal$$reject(promise, reason);
|
522
|
+
return promise;
|
523
|
+
};
|
524
|
+
|
525
|
+
var $$es6$promise$promise$$counter = 0;
|
526
|
+
|
527
|
+
function $$es6$promise$promise$$needsResolver() {
|
528
|
+
throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
|
529
|
+
}
|
530
|
+
|
531
|
+
function $$es6$promise$promise$$needsNew() {
|
532
|
+
throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
|
533
|
+
}
|
534
|
+
|
535
|
+
var $$es6$promise$promise$$default = $$es6$promise$promise$$Promise;
|
536
|
+
|
537
|
+
/**
|
538
|
+
Promise objects represent the eventual result of an asynchronous operation. The
|
539
|
+
primary way of interacting with a promise is through its `then` method, which
|
540
|
+
registers callbacks to receive either a promise’s eventual value or the reason
|
541
|
+
why the promise cannot be fulfilled.
|
542
|
+
|
543
|
+
Terminology
|
544
|
+
-----------
|
545
|
+
|
546
|
+
- `promise` is an object or function with a `then` method whose behavior conforms to this specification.
|
547
|
+
- `thenable` is an object or function that defines a `then` method.
|
548
|
+
- `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
|
549
|
+
- `exception` is a value that is thrown using the throw statement.
|
550
|
+
- `reason` is a value that indicates why a promise was rejected.
|
551
|
+
- `settled` the final resting state of a promise, fulfilled or rejected.
|
552
|
+
|
553
|
+
A promise can be in one of three states: pending, fulfilled, or rejected.
|
554
|
+
|
555
|
+
Promises that are fulfilled have a fulfillment value and are in the fulfilled
|
556
|
+
state. Promises that are rejected have a rejection reason and are in the
|
557
|
+
rejected state. A fulfillment value is never a thenable.
|
558
|
+
|
559
|
+
Promises can also be said to *resolve* a value. If this value is also a
|
560
|
+
promise, then the original promise's settled state will match the value's
|
561
|
+
settled state. So a promise that *resolves* a promise that rejects will
|
562
|
+
itself reject, and a promise that *resolves* a promise that fulfills will
|
563
|
+
itself fulfill.
|
564
|
+
|
565
|
+
|
566
|
+
Basic Usage:
|
567
|
+
------------
|
568
|
+
|
569
|
+
```js
|
570
|
+
var promise = new Promise(function(resolve, reject) {
|
571
|
+
// on success
|
572
|
+
resolve(value);
|
573
|
+
|
574
|
+
// on failure
|
575
|
+
reject(reason);
|
576
|
+
});
|
577
|
+
|
578
|
+
promise.then(function(value) {
|
579
|
+
// on fulfillment
|
580
|
+
}, function(reason) {
|
581
|
+
// on rejection
|
582
|
+
});
|
583
|
+
```
|
584
|
+
|
585
|
+
Advanced Usage:
|
586
|
+
---------------
|
587
|
+
|
588
|
+
Promises shine when abstracting away asynchronous interactions such as
|
589
|
+
`XMLHttpRequest`s.
|
590
|
+
|
591
|
+
```js
|
592
|
+
function getJSON(url) {
|
593
|
+
return new Promise(function(resolve, reject){
|
594
|
+
var xhr = new XMLHttpRequest();
|
595
|
+
|
596
|
+
xhr.open('GET', url);
|
597
|
+
xhr.onreadystatechange = handler;
|
598
|
+
xhr.responseType = 'json';
|
599
|
+
xhr.setRequestHeader('Accept', 'application/json');
|
600
|
+
xhr.send();
|
601
|
+
|
602
|
+
function handler() {
|
603
|
+
if (this.readyState === this.DONE) {
|
604
|
+
if (this.status === 200) {
|
605
|
+
resolve(this.response);
|
606
|
+
} else {
|
607
|
+
reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));
|
608
|
+
}
|
609
|
+
}
|
610
|
+
};
|
611
|
+
});
|
612
|
+
}
|
613
|
+
|
614
|
+
getJSON('/posts.json').then(function(json) {
|
615
|
+
// on fulfillment
|
616
|
+
}, function(reason) {
|
617
|
+
// on rejection
|
618
|
+
});
|
619
|
+
```
|
620
|
+
|
621
|
+
Unlike callbacks, promises are great composable primitives.
|
622
|
+
|
623
|
+
```js
|
624
|
+
Promise.all([
|
625
|
+
getJSON('/posts'),
|
626
|
+
getJSON('/comments')
|
627
|
+
]).then(function(values){
|
628
|
+
values[0] // => postsJSON
|
629
|
+
values[1] // => commentsJSON
|
630
|
+
|
631
|
+
return values;
|
632
|
+
});
|
633
|
+
```
|
634
|
+
|
635
|
+
@class Promise
|
636
|
+
@param {function} resolver
|
637
|
+
@param {String} label optional string for labeling the promise.
|
638
|
+
Useful for tooling.
|
639
|
+
@constructor
|
640
|
+
*/
|
641
|
+
function $$es6$promise$promise$$Promise(resolver, label) {
|
642
|
+
this._id = $$es6$promise$promise$$counter++;
|
643
|
+
this._label = label;
|
644
|
+
this._state = undefined;
|
645
|
+
this._result = undefined;
|
646
|
+
this._subscribers = [];
|
647
|
+
|
648
|
+
if ($$$internal$$noop !== resolver) {
|
649
|
+
if (!$$utils$$isFunction(resolver)) {
|
650
|
+
$$es6$promise$promise$$needsResolver();
|
651
|
+
}
|
652
|
+
|
653
|
+
if (!(this instanceof $$es6$promise$promise$$Promise)) {
|
654
|
+
$$es6$promise$promise$$needsNew();
|
655
|
+
}
|
656
|
+
|
657
|
+
$$$internal$$initializePromise(this, resolver);
|
658
|
+
}
|
659
|
+
}
|
660
|
+
|
661
|
+
$$es6$promise$promise$$Promise.all = $$promise$all$$default;
|
662
|
+
$$es6$promise$promise$$Promise.race = $$promise$race$$default;
|
663
|
+
$$es6$promise$promise$$Promise.resolve = $$promise$resolve$$default;
|
664
|
+
$$es6$promise$promise$$Promise.reject = $$promise$reject$$default;
|
665
|
+
|
666
|
+
$$es6$promise$promise$$Promise.prototype = {
|
667
|
+
constructor: $$es6$promise$promise$$Promise,
|
668
|
+
|
669
|
+
/**
|
670
|
+
The primary way of interacting with a promise is through its `then` method,
|
671
|
+
which registers callbacks to receive either a promise's eventual value or the
|
672
|
+
reason why the promise cannot be fulfilled.
|
673
|
+
|
674
|
+
```js
|
675
|
+
findUser().then(function(user){
|
676
|
+
// user is available
|
677
|
+
}, function(reason){
|
678
|
+
// user is unavailable, and you are given the reason why
|
679
|
+
});
|
680
|
+
```
|
681
|
+
|
682
|
+
Chaining
|
683
|
+
--------
|
684
|
+
|
685
|
+
The return value of `then` is itself a promise. This second, 'downstream'
|
686
|
+
promise is resolved with the return value of the first promise's fulfillment
|
687
|
+
or rejection handler, or rejected if the handler throws an exception.
|
688
|
+
|
689
|
+
```js
|
690
|
+
findUser().then(function (user) {
|
691
|
+
return user.name;
|
692
|
+
}, function (reason) {
|
693
|
+
return 'default name';
|
694
|
+
}).then(function (userName) {
|
695
|
+
// If `findUser` fulfilled, `userName` will be the user's name, otherwise it
|
696
|
+
// will be `'default name'`
|
697
|
+
});
|
698
|
+
|
699
|
+
findUser().then(function (user) {
|
700
|
+
throw new Error('Found user, but still unhappy');
|
701
|
+
}, function (reason) {
|
702
|
+
throw new Error('`findUser` rejected and we're unhappy');
|
703
|
+
}).then(function (value) {
|
704
|
+
// never reached
|
705
|
+
}, function (reason) {
|
706
|
+
// if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
|
707
|
+
// If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
|
708
|
+
});
|
709
|
+
```
|
710
|
+
If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
|
711
|
+
|
712
|
+
```js
|
713
|
+
findUser().then(function (user) {
|
714
|
+
throw new PedagogicalException('Upstream error');
|
715
|
+
}).then(function (value) {
|
716
|
+
// never reached
|
717
|
+
}).then(function (value) {
|
718
|
+
// never reached
|
719
|
+
}, function (reason) {
|
720
|
+
// The `PedgagocialException` is propagated all the way down to here
|
721
|
+
});
|
722
|
+
```
|
723
|
+
|
724
|
+
Assimilation
|
725
|
+
------------
|
726
|
+
|
727
|
+
Sometimes the value you want to propagate to a downstream promise can only be
|
728
|
+
retrieved asynchronously. This can be achieved by returning a promise in the
|
729
|
+
fulfillment or rejection handler. The downstream promise will then be pending
|
730
|
+
until the returned promise is settled. This is called *assimilation*.
|
731
|
+
|
732
|
+
```js
|
733
|
+
findUser().then(function (user) {
|
734
|
+
return findCommentsByAuthor(user);
|
735
|
+
}).then(function (comments) {
|
736
|
+
// The user's comments are now available
|
737
|
+
});
|
738
|
+
```
|
739
|
+
|
740
|
+
If the assimliated promise rejects, then the downstream promise will also reject.
|
741
|
+
|
742
|
+
```js
|
743
|
+
findUser().then(function (user) {
|
744
|
+
return findCommentsByAuthor(user);
|
745
|
+
}).then(function (comments) {
|
746
|
+
// If `findCommentsByAuthor` fulfills, we'll have the value here
|
747
|
+
}, function (reason) {
|
748
|
+
// If `findCommentsByAuthor` rejects, we'll have the reason here
|
749
|
+
});
|
750
|
+
```
|
751
|
+
|
752
|
+
Simple Example
|
753
|
+
--------------
|
754
|
+
|
755
|
+
Synchronous Example
|
756
|
+
|
757
|
+
```javascript
|
758
|
+
var result;
|
759
|
+
|
760
|
+
try {
|
761
|
+
result = findResult();
|
762
|
+
// success
|
763
|
+
} catch(reason) {
|
764
|
+
// failure
|
765
|
+
}
|
766
|
+
```
|
767
|
+
|
768
|
+
Errback Example
|
769
|
+
|
770
|
+
```js
|
771
|
+
findResult(function(result, err){
|
772
|
+
if (err) {
|
773
|
+
// failure
|
774
|
+
} else {
|
775
|
+
// success
|
776
|
+
}
|
777
|
+
});
|
778
|
+
```
|
779
|
+
|
780
|
+
Promise Example;
|
781
|
+
|
782
|
+
```javascript
|
783
|
+
findResult().then(function(result){
|
784
|
+
// success
|
785
|
+
}, function(reason){
|
786
|
+
// failure
|
787
|
+
});
|
788
|
+
```
|
789
|
+
|
790
|
+
Advanced Example
|
791
|
+
--------------
|
792
|
+
|
793
|
+
Synchronous Example
|
794
|
+
|
795
|
+
```javascript
|
796
|
+
var author, books;
|
797
|
+
|
798
|
+
try {
|
799
|
+
author = findAuthor();
|
800
|
+
books = findBooksByAuthor(author);
|
801
|
+
// success
|
802
|
+
} catch(reason) {
|
803
|
+
// failure
|
804
|
+
}
|
805
|
+
```
|
806
|
+
|
807
|
+
Errback Example
|
808
|
+
|
809
|
+
```js
|
810
|
+
|
811
|
+
function foundBooks(books) {
|
812
|
+
|
813
|
+
}
|
814
|
+
|
815
|
+
function failure(reason) {
|
816
|
+
|
817
|
+
}
|
818
|
+
|
819
|
+
findAuthor(function(author, err){
|
820
|
+
if (err) {
|
821
|
+
failure(err);
|
822
|
+
// failure
|
823
|
+
} else {
|
824
|
+
try {
|
825
|
+
findBoooksByAuthor(author, function(books, err) {
|
826
|
+
if (err) {
|
827
|
+
failure(err);
|
828
|
+
} else {
|
829
|
+
try {
|
830
|
+
foundBooks(books);
|
831
|
+
} catch(reason) {
|
832
|
+
failure(reason);
|
833
|
+
}
|
834
|
+
}
|
835
|
+
});
|
836
|
+
} catch(error) {
|
837
|
+
failure(err);
|
838
|
+
}
|
839
|
+
// success
|
840
|
+
}
|
841
|
+
});
|
842
|
+
```
|
843
|
+
|
844
|
+
Promise Example;
|
845
|
+
|
846
|
+
```javascript
|
847
|
+
findAuthor().
|
848
|
+
then(findBooksByAuthor).
|
849
|
+
then(function(books){
|
850
|
+
// found books
|
851
|
+
}).catch(function(reason){
|
852
|
+
// something went wrong
|
853
|
+
});
|
854
|
+
```
|
855
|
+
|
856
|
+
@method then
|
857
|
+
@param {Function} onFulfilled
|
858
|
+
@param {Function} onRejected
|
859
|
+
@param {String} label optional string for labeling the promise.
|
860
|
+
Useful for tooling.
|
861
|
+
@return {Promise}
|
862
|
+
*/
|
863
|
+
then: function(onFulfillment, onRejection, label) {
|
864
|
+
var parent = this;
|
865
|
+
var state = parent._state;
|
866
|
+
|
867
|
+
if (state === $$$internal$$FULFILLED && !onFulfillment || state === $$$internal$$REJECTED && !onRejection) {
|
868
|
+
return this;
|
869
|
+
}
|
870
|
+
|
871
|
+
parent._onerror = null;
|
872
|
+
|
873
|
+
var child = new this.constructor($$$internal$$noop, label);
|
874
|
+
var result = parent._result;
|
875
|
+
|
876
|
+
if (state) {
|
877
|
+
var callback = arguments[state - 1];
|
878
|
+
$$asap$$default(function(){
|
879
|
+
$$$internal$$invokeCallback(state, child, callback, result);
|
880
|
+
});
|
881
|
+
} else {
|
882
|
+
$$$internal$$subscribe(parent, child, onFulfillment, onRejection);
|
883
|
+
}
|
884
|
+
|
885
|
+
return child;
|
886
|
+
},
|
887
|
+
|
888
|
+
/**
|
889
|
+
`catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
|
890
|
+
as the catch block of a try/catch statement.
|
891
|
+
|
892
|
+
```js
|
893
|
+
function findAuthor(){
|
894
|
+
throw new Error('couldn't find that author');
|
895
|
+
}
|
896
|
+
|
897
|
+
// synchronous
|
898
|
+
try {
|
899
|
+
findAuthor();
|
900
|
+
} catch(reason) {
|
901
|
+
// something went wrong
|
902
|
+
}
|
903
|
+
|
904
|
+
// async with promises
|
905
|
+
findAuthor().catch(function(reason){
|
906
|
+
// something went wrong
|
907
|
+
});
|
908
|
+
```
|
909
|
+
|
910
|
+
@method catch
|
911
|
+
@param {Function} onRejection
|
912
|
+
@param {String} label optional string for labeling the promise.
|
913
|
+
Useful for tooling.
|
914
|
+
@return {Promise}
|
915
|
+
*/
|
916
|
+
'catch': function(onRejection, label) {
|
917
|
+
return this.then(null, onRejection, label);
|
918
|
+
}
|
919
|
+
};
|
920
|
+
|
921
|
+
var $$es6$promise$polyfill$$default = function polyfill() {
|
922
|
+
var local;
|
923
|
+
|
924
|
+
if (typeof global !== 'undefined') {
|
925
|
+
local = global;
|
926
|
+
} else if (typeof window !== 'undefined' && window.document) {
|
927
|
+
local = window;
|
928
|
+
} else {
|
929
|
+
local = self;
|
930
|
+
}
|
931
|
+
|
932
|
+
var es6PromiseSupport =
|
933
|
+
"Promise" in local &&
|
934
|
+
// Some of these methods are missing from
|
935
|
+
// Firefox/Chrome experimental implementations
|
936
|
+
"resolve" in local.Promise &&
|
937
|
+
"reject" in local.Promise &&
|
938
|
+
"all" in local.Promise &&
|
939
|
+
"race" in local.Promise &&
|
940
|
+
// Older version of the spec had a resolver object
|
941
|
+
// as the arg rather than a function
|
942
|
+
(function() {
|
943
|
+
var resolve;
|
944
|
+
new local.Promise(function(r) { resolve = r; });
|
945
|
+
return $$utils$$isFunction(resolve);
|
946
|
+
}());
|
947
|
+
|
948
|
+
if (!es6PromiseSupport) {
|
949
|
+
local.Promise = $$es6$promise$promise$$default;
|
950
|
+
}
|
951
|
+
};
|
952
|
+
|
953
|
+
var es6$promise$umd$$ES6Promise = {
|
954
|
+
Promise: $$es6$promise$promise$$default,
|
955
|
+
polyfill: $$es6$promise$polyfill$$default
|
956
|
+
};
|
957
|
+
|
958
|
+
/* global define:true module:true window: true */
|
959
|
+
if (typeof define === 'function' && define['amd']) {
|
960
|
+
define(function() { return es6$promise$umd$$ES6Promise; });
|
961
|
+
} else if (typeof module !== 'undefined' && module['exports']) {
|
962
|
+
module['exports'] = es6$promise$umd$$ES6Promise;
|
963
|
+
} else if (typeof this !== 'undefined') {
|
964
|
+
this['ES6Promise'] = es6$promise$umd$$ES6Promise;
|
965
|
+
}
|
966
|
+
}).call(this);
|