es6-promise-rails 2.0.2.1 → 2.1.0

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.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es6-promise-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonid Beder
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-08 00:00:00.000000000 Z
11
+ date: 2015-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -33,14 +33,13 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - ".gitignore"
35
35
  - Gemfile
36
- - Gemfile.lock
37
36
  - LICENSE
38
37
  - README.md
39
38
  - Rakefile
40
39
  - es6-promise-rails.gemspec
41
40
  - lib/es6/promise/rails.rb
42
41
  - lib/es6/promise/rails/version.rb
43
- - vendor/assets/javascripts/es6-promise.js
42
+ - vendor/assets/javascripts/promise.js
44
43
  homepage: ''
45
44
  licenses:
46
45
  - MIT
@@ -61,8 +60,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
60
  version: '0'
62
61
  requirements: []
63
62
  rubyforge_project:
64
- rubygems_version: 2.4.1
63
+ rubygems_version: 2.4.6
65
64
  signing_key:
66
65
  specification_version: 4
67
66
  summary: A polyfill for ES6-style Promises with integration for Rails
68
67
  test_files: []
68
+ has_rdoc:
data/Gemfile.lock DELETED
@@ -1,48 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- es6-promise-rails (2.0.2.1)
5
- railties (>= 3.1.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actionpack (4.1.8)
11
- actionview (= 4.1.8)
12
- activesupport (= 4.1.8)
13
- rack (~> 1.5.2)
14
- rack-test (~> 0.6.2)
15
- actionview (4.1.8)
16
- activesupport (= 4.1.8)
17
- builder (~> 3.1)
18
- erubis (~> 2.7.0)
19
- activesupport (4.1.8)
20
- i18n (~> 0.6, >= 0.6.9)
21
- json (~> 1.7, >= 1.7.7)
22
- minitest (~> 5.1)
23
- thread_safe (~> 0.1)
24
- tzinfo (~> 1.1)
25
- builder (3.2.2)
26
- erubis (2.7.0)
27
- i18n (0.6.11)
28
- json (1.8.1)
29
- minitest (5.4.3)
30
- rack (1.5.2)
31
- rack-test (0.6.2)
32
- rack (>= 1.0)
33
- railties (4.1.8)
34
- actionpack (= 4.1.8)
35
- activesupport (= 4.1.8)
36
- rake (>= 0.8.7)
37
- thor (>= 0.18.1, < 2.0)
38
- rake (10.4.2)
39
- thor (0.19.1)
40
- thread_safe (0.3.4)
41
- tzinfo (1.2.2)
42
- thread_safe (~> 0.1)
43
-
44
- PLATFORMS
45
- ruby
46
-
47
- DEPENDENCIES
48
- es6-promise-rails!
@@ -1,960 +0,0 @@
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
- Useful for tooling.
638
- @constructor
639
- */
640
- function $$es6$promise$promise$$Promise(resolver) {
641
- this._id = $$es6$promise$promise$$counter++;
642
- this._state = undefined;
643
- this._result = undefined;
644
- this._subscribers = [];
645
-
646
- if ($$$internal$$noop !== resolver) {
647
- if (!$$utils$$isFunction(resolver)) {
648
- $$es6$promise$promise$$needsResolver();
649
- }
650
-
651
- if (!(this instanceof $$es6$promise$promise$$Promise)) {
652
- $$es6$promise$promise$$needsNew();
653
- }
654
-
655
- $$$internal$$initializePromise(this, resolver);
656
- }
657
- }
658
-
659
- $$es6$promise$promise$$Promise.all = $$promise$all$$default;
660
- $$es6$promise$promise$$Promise.race = $$promise$race$$default;
661
- $$es6$promise$promise$$Promise.resolve = $$promise$resolve$$default;
662
- $$es6$promise$promise$$Promise.reject = $$promise$reject$$default;
663
-
664
- $$es6$promise$promise$$Promise.prototype = {
665
- constructor: $$es6$promise$promise$$Promise,
666
-
667
- /**
668
- The primary way of interacting with a promise is through its `then` method,
669
- which registers callbacks to receive either a promise's eventual value or the
670
- reason why the promise cannot be fulfilled.
671
-
672
- ```js
673
- findUser().then(function(user){
674
- // user is available
675
- }, function(reason){
676
- // user is unavailable, and you are given the reason why
677
- });
678
- ```
679
-
680
- Chaining
681
- --------
682
-
683
- The return value of `then` is itself a promise. This second, 'downstream'
684
- promise is resolved with the return value of the first promise's fulfillment
685
- or rejection handler, or rejected if the handler throws an exception.
686
-
687
- ```js
688
- findUser().then(function (user) {
689
- return user.name;
690
- }, function (reason) {
691
- return 'default name';
692
- }).then(function (userName) {
693
- // If `findUser` fulfilled, `userName` will be the user's name, otherwise it
694
- // will be `'default name'`
695
- });
696
-
697
- findUser().then(function (user) {
698
- throw new Error('Found user, but still unhappy');
699
- }, function (reason) {
700
- throw new Error('`findUser` rejected and we're unhappy');
701
- }).then(function (value) {
702
- // never reached
703
- }, function (reason) {
704
- // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
705
- // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
706
- });
707
- ```
708
- If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
709
-
710
- ```js
711
- findUser().then(function (user) {
712
- throw new PedagogicalException('Upstream error');
713
- }).then(function (value) {
714
- // never reached
715
- }).then(function (value) {
716
- // never reached
717
- }, function (reason) {
718
- // The `PedgagocialException` is propagated all the way down to here
719
- });
720
- ```
721
-
722
- Assimilation
723
- ------------
724
-
725
- Sometimes the value you want to propagate to a downstream promise can only be
726
- retrieved asynchronously. This can be achieved by returning a promise in the
727
- fulfillment or rejection handler. The downstream promise will then be pending
728
- until the returned promise is settled. This is called *assimilation*.
729
-
730
- ```js
731
- findUser().then(function (user) {
732
- return findCommentsByAuthor(user);
733
- }).then(function (comments) {
734
- // The user's comments are now available
735
- });
736
- ```
737
-
738
- If the assimliated promise rejects, then the downstream promise will also reject.
739
-
740
- ```js
741
- findUser().then(function (user) {
742
- return findCommentsByAuthor(user);
743
- }).then(function (comments) {
744
- // If `findCommentsByAuthor` fulfills, we'll have the value here
745
- }, function (reason) {
746
- // If `findCommentsByAuthor` rejects, we'll have the reason here
747
- });
748
- ```
749
-
750
- Simple Example
751
- --------------
752
-
753
- Synchronous Example
754
-
755
- ```javascript
756
- var result;
757
-
758
- try {
759
- result = findResult();
760
- // success
761
- } catch(reason) {
762
- // failure
763
- }
764
- ```
765
-
766
- Errback Example
767
-
768
- ```js
769
- findResult(function(result, err){
770
- if (err) {
771
- // failure
772
- } else {
773
- // success
774
- }
775
- });
776
- ```
777
-
778
- Promise Example;
779
-
780
- ```javascript
781
- findResult().then(function(result){
782
- // success
783
- }, function(reason){
784
- // failure
785
- });
786
- ```
787
-
788
- Advanced Example
789
- --------------
790
-
791
- Synchronous Example
792
-
793
- ```javascript
794
- var author, books;
795
-
796
- try {
797
- author = findAuthor();
798
- books = findBooksByAuthor(author);
799
- // success
800
- } catch(reason) {
801
- // failure
802
- }
803
- ```
804
-
805
- Errback Example
806
-
807
- ```js
808
-
809
- function foundBooks(books) {
810
-
811
- }
812
-
813
- function failure(reason) {
814
-
815
- }
816
-
817
- findAuthor(function(author, err){
818
- if (err) {
819
- failure(err);
820
- // failure
821
- } else {
822
- try {
823
- findBoooksByAuthor(author, function(books, err) {
824
- if (err) {
825
- failure(err);
826
- } else {
827
- try {
828
- foundBooks(books);
829
- } catch(reason) {
830
- failure(reason);
831
- }
832
- }
833
- });
834
- } catch(error) {
835
- failure(err);
836
- }
837
- // success
838
- }
839
- });
840
- ```
841
-
842
- Promise Example;
843
-
844
- ```javascript
845
- findAuthor().
846
- then(findBooksByAuthor).
847
- then(function(books){
848
- // found books
849
- }).catch(function(reason){
850
- // something went wrong
851
- });
852
- ```
853
-
854
- @method then
855
- @param {Function} onFulfilled
856
- @param {Function} onRejected
857
- Useful for tooling.
858
- @return {Promise}
859
- */
860
- then: function(onFulfillment, onRejection) {
861
- var parent = this;
862
- var state = parent._state;
863
-
864
- if (state === $$$internal$$FULFILLED && !onFulfillment || state === $$$internal$$REJECTED && !onRejection) {
865
- return this;
866
- }
867
-
868
- var child = new this.constructor($$$internal$$noop);
869
- var result = parent._result;
870
-
871
- if (state) {
872
- var callback = arguments[state - 1];
873
- $$asap$$default(function(){
874
- $$$internal$$invokeCallback(state, child, callback, result);
875
- });
876
- } else {
877
- $$$internal$$subscribe(parent, child, onFulfillment, onRejection);
878
- }
879
-
880
- return child;
881
- },
882
-
883
- /**
884
- `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
885
- as the catch block of a try/catch statement.
886
-
887
- ```js
888
- function findAuthor(){
889
- throw new Error('couldn't find that author');
890
- }
891
-
892
- // synchronous
893
- try {
894
- findAuthor();
895
- } catch(reason) {
896
- // something went wrong
897
- }
898
-
899
- // async with promises
900
- findAuthor().catch(function(reason){
901
- // something went wrong
902
- });
903
- ```
904
-
905
- @method catch
906
- @param {Function} onRejection
907
- Useful for tooling.
908
- @return {Promise}
909
- */
910
- 'catch': function(onRejection) {
911
- return this.then(null, onRejection);
912
- }
913
- };
914
-
915
- var $$es6$promise$polyfill$$default = function polyfill() {
916
- var local;
917
-
918
- if (typeof global !== 'undefined') {
919
- local = global;
920
- } else if (typeof window !== 'undefined' && window.document) {
921
- local = window;
922
- } else {
923
- local = self;
924
- }
925
-
926
- var es6PromiseSupport =
927
- "Promise" in local &&
928
- // Some of these methods are missing from
929
- // Firefox/Chrome experimental implementations
930
- "resolve" in local.Promise &&
931
- "reject" in local.Promise &&
932
- "all" in local.Promise &&
933
- "race" in local.Promise &&
934
- // Older version of the spec had a resolver object
935
- // as the arg rather than a function
936
- (function() {
937
- var resolve;
938
- new local.Promise(function(r) { resolve = r; });
939
- return $$utils$$isFunction(resolve);
940
- }());
941
-
942
- if (!es6PromiseSupport) {
943
- local.Promise = $$es6$promise$promise$$default;
944
- }
945
- };
946
-
947
- var es6$promise$umd$$ES6Promise = {
948
- 'Promise': $$es6$promise$promise$$default,
949
- 'polyfill': $$es6$promise$polyfill$$default
950
- };
951
-
952
- /* global define:true module:true window: true */
953
- if (typeof define === 'function' && define['amd']) {
954
- define(function() { return es6$promise$umd$$ES6Promise; });
955
- } else if (typeof module !== 'undefined' && module['exports']) {
956
- module['exports'] = es6$promise$umd$$ES6Promise;
957
- } else if (typeof this !== 'undefined') {
958
- this['ES6Promise'] = es6$promise$umd$$ES6Promise;
959
- }
960
- }).call(this);