es6_promise_polyfill_rails 0.1.3 → 0.1.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f76ff4726e4ad1058b3bdf01bcb7ae0c452158a4
4
- data.tar.gz: 6766e65ba5dc070cf561a692eec6074c6684e5cc
3
+ metadata.gz: ea1d8c41e9ef3e132e7a20a6380594ffedb86ccc
4
+ data.tar.gz: 14fa153dcf796b24a6ce94c4549136ca17f6d3b0
5
5
  SHA512:
6
- metadata.gz: f6a80aff08788a481417c5d584f492b01ceb297c7788a6d02e12a2a37f071f072aecdce8fde1329226bb19189d6d73af7b5a6932efb1a4e8ff27d80369983d1b
7
- data.tar.gz: 4940a27d4b54f903c0b6bd239e0979432c098d460d2cae1ba1cd04de31b3fd51a5165da8aca0a305aeececd06b721e1d7ca1f2cc9886f596cf020bece8b76ac8
6
+ metadata.gz: 323dc011357a555ec6d4c6604e5de4d717fd8b20360739b67b0b813da8d6187b4bd49835224022e6700b2db85054dee09c4802485bb66c2ecde8119b547e9f22
7
+ data.tar.gz: 3851ae206cefd622ecb3614fbd0827dc9a2d6f187efb764edb94c22c3c5a6569b3bbdd7337db44ea17955fdf33f9387a8ea9fa67260405115b5320d21759eea3
@@ -1,4 +1,12 @@
1
1
  if(typeof Promise != 'function'){
2
+ /*!
3
+ * @overview es6-promise - a tiny implementation of Promises/A+.
4
+ * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
5
+ * @license Licensed under MIT license
6
+ * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE
7
+ * @version 3.2.1
8
+ */
9
+
2
10
  (function() {
3
11
  "use strict";
4
12
  function lib$es6$promise$utils$$objectOrFunction(x) {
@@ -24,7 +32,6 @@ if(typeof Promise != 'function'){
24
32
 
25
33
  var lib$es6$promise$utils$$isArray = lib$es6$promise$utils$$_isArray;
26
34
  var lib$es6$promise$asap$$len = 0;
27
- var lib$es6$promise$asap$$toString = {}.toString;
28
35
  var lib$es6$promise$asap$$vertxNext;
29
36
  var lib$es6$promise$asap$$customSchedulerFn;
30
37
 
@@ -55,7 +62,7 @@ if(typeof Promise != 'function'){
55
62
  var lib$es6$promise$asap$$browserWindow = (typeof window !== 'undefined') ? window : undefined;
56
63
  var lib$es6$promise$asap$$browserGlobal = lib$es6$promise$asap$$browserWindow || {};
57
64
  var lib$es6$promise$asap$$BrowserMutationObserver = lib$es6$promise$asap$$browserGlobal.MutationObserver || lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver;
58
- var lib$es6$promise$asap$$isNode = typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
65
+ var lib$es6$promise$asap$$isNode = typeof self === 'undefined' && typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
59
66
 
60
67
  // test for web worker but not in IE10
61
68
  var lib$es6$promise$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' &&
@@ -143,6 +150,43 @@ if(typeof Promise != 'function'){
143
150
  } else {
144
151
  lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useSetTimeout();
145
152
  }
153
+ function lib$es6$promise$then$$then(onFulfillment, onRejection) {
154
+ var parent = this;
155
+
156
+ var child = new this.constructor(lib$es6$promise$$internal$$noop);
157
+
158
+ if (child[lib$es6$promise$$internal$$PROMISE_ID] === undefined) {
159
+ lib$es6$promise$$internal$$makePromise(child);
160
+ }
161
+
162
+ var state = parent._state;
163
+
164
+ if (state) {
165
+ var callback = arguments[state - 1];
166
+ lib$es6$promise$asap$$asap(function(){
167
+ lib$es6$promise$$internal$$invokeCallback(state, child, callback, parent._result);
168
+ });
169
+ } else {
170
+ lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection);
171
+ }
172
+
173
+ return child;
174
+ }
175
+ var lib$es6$promise$then$$default = lib$es6$promise$then$$then;
176
+ function lib$es6$promise$promise$resolve$$resolve(object) {
177
+ /*jshint validthis:true */
178
+ var Constructor = this;
179
+
180
+ if (object && typeof object === 'object' && object.constructor === Constructor) {
181
+ return object;
182
+ }
183
+
184
+ var promise = new Constructor(lib$es6$promise$$internal$$noop);
185
+ lib$es6$promise$$internal$$resolve(promise, object);
186
+ return promise;
187
+ }
188
+ var lib$es6$promise$promise$resolve$$default = lib$es6$promise$promise$resolve$$resolve;
189
+ var lib$es6$promise$$internal$$PROMISE_ID = Math.random().toString(36).substring(16);
146
190
 
147
191
  function lib$es6$promise$$internal$$noop() {}
148
192
 
@@ -216,12 +260,12 @@ if(typeof Promise != 'function'){
216
260
  }
217
261
  }
218
262
 
219
- function lib$es6$promise$$internal$$handleMaybeThenable(promise, maybeThenable) {
220
- if (maybeThenable.constructor === promise.constructor) {
263
+ function lib$es6$promise$$internal$$handleMaybeThenable(promise, maybeThenable, then) {
264
+ if (maybeThenable.constructor === promise.constructor &&
265
+ then === lib$es6$promise$then$$default &&
266
+ constructor.resolve === lib$es6$promise$promise$resolve$$default) {
221
267
  lib$es6$promise$$internal$$handleOwnThenable(promise, maybeThenable);
222
268
  } else {
223
- var then = lib$es6$promise$$internal$$getThen(maybeThenable);
224
-
225
269
  if (then === lib$es6$promise$$internal$$GET_THEN_ERROR) {
226
270
  lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$GET_THEN_ERROR.error);
227
271
  } else if (then === undefined) {
@@ -238,7 +282,7 @@ if(typeof Promise != 'function'){
238
282
  if (promise === value) {
239
283
  lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$selfFulfillment());
240
284
  } else if (lib$es6$promise$utils$$objectOrFunction(value)) {
241
- lib$es6$promise$$internal$$handleMaybeThenable(promise, value);
285
+ lib$es6$promise$$internal$$handleMaybeThenable(promise, value, lib$es6$promise$$internal$$getThen(value));
242
286
  } else {
243
287
  lib$es6$promise$$internal$$fulfill(promise, value);
244
288
  }
@@ -373,104 +417,18 @@ if(typeof Promise != 'function'){
373
417
  }
374
418
  }
375
419
 
376
- function lib$es6$promise$enumerator$$Enumerator(Constructor, input) {
377
- var enumerator = this;
378
-
379
- enumerator._instanceConstructor = Constructor;
380
- enumerator.promise = new Constructor(lib$es6$promise$$internal$$noop);
381
-
382
- if (enumerator._validateInput(input)) {
383
- enumerator._input = input;
384
- enumerator.length = input.length;
385
- enumerator._remaining = input.length;
386
-
387
- enumerator._init();
388
-
389
- if (enumerator.length === 0) {
390
- lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result);
391
- } else {
392
- enumerator.length = enumerator.length || 0;
393
- enumerator._enumerate();
394
- if (enumerator._remaining === 0) {
395
- lib$es6$promise$$internal$$fulfill(enumerator.promise, enumerator._result);
396
- }
397
- }
398
- } else {
399
- lib$es6$promise$$internal$$reject(enumerator.promise, enumerator._validationError());
400
- }
420
+ var lib$es6$promise$$internal$$id = 0;
421
+ function lib$es6$promise$$internal$$nextId() {
422
+ return lib$es6$promise$$internal$$id++;
401
423
  }
402
424
 
403
- lib$es6$promise$enumerator$$Enumerator.prototype._validateInput = function(input) {
404
- return lib$es6$promise$utils$$isArray(input);
405
- };
406
-
407
- lib$es6$promise$enumerator$$Enumerator.prototype._validationError = function() {
408
- return new Error('Array Methods must be provided an Array');
409
- };
410
-
411
- lib$es6$promise$enumerator$$Enumerator.prototype._init = function() {
412
- this._result = new Array(this.length);
413
- };
414
-
415
- var lib$es6$promise$enumerator$$default = lib$es6$promise$enumerator$$Enumerator;
416
-
417
- lib$es6$promise$enumerator$$Enumerator.prototype._enumerate = function() {
418
- var enumerator = this;
419
-
420
- var length = enumerator.length;
421
- var promise = enumerator.promise;
422
- var input = enumerator._input;
423
-
424
- for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
425
- enumerator._eachEntry(input[i], i);
426
- }
427
- };
428
-
429
- lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) {
430
- var enumerator = this;
431
- var c = enumerator._instanceConstructor;
432
-
433
- if (lib$es6$promise$utils$$isMaybeThenable(entry)) {
434
- if (entry.constructor === c && entry._state !== lib$es6$promise$$internal$$PENDING) {
435
- entry._onerror = null;
436
- enumerator._settledAt(entry._state, i, entry._result);
437
- } else {
438
- enumerator._willSettleAt(c.resolve(entry), i);
439
- }
440
- } else {
441
- enumerator._remaining--;
442
- enumerator._result[i] = entry;
443
- }
444
- };
445
-
446
- lib$es6$promise$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) {
447
- var enumerator = this;
448
- var promise = enumerator.promise;
449
-
450
- if (promise._state === lib$es6$promise$$internal$$PENDING) {
451
- enumerator._remaining--;
452
-
453
- if (state === lib$es6$promise$$internal$$REJECTED) {
454
- lib$es6$promise$$internal$$reject(promise, value);
455
- } else {
456
- enumerator._result[i] = value;
457
- }
458
- }
459
-
460
- if (enumerator._remaining === 0) {
461
- lib$es6$promise$$internal$$fulfill(promise, enumerator._result);
462
- }
463
- };
464
-
465
- lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) {
466
- var enumerator = this;
425
+ function lib$es6$promise$$internal$$makePromise(promise) {
426
+ promise[lib$es6$promise$$internal$$PROMISE_ID] = lib$es6$promise$$internal$$id++;
427
+ promise._state = undefined;
428
+ promise._result = undefined;
429
+ promise._subscribers = [];
430
+ }
467
431
 
468
- lib$es6$promise$$internal$$subscribe(promise, undefined, function(value) {
469
- enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED, i, value);
470
- }, function(reason) {
471
- enumerator._settledAt(lib$es6$promise$$internal$$REJECTED, i, reason);
472
- });
473
- };
474
432
  function lib$es6$promise$promise$all$$all(entries) {
475
433
  return new lib$es6$promise$enumerator$$default(this, entries).promise;
476
434
  }
@@ -479,43 +437,20 @@ if(typeof Promise != 'function'){
479
437
  /*jshint validthis:true */
480
438
  var Constructor = this;
481
439
 
482
- var promise = new Constructor(lib$es6$promise$$internal$$noop);
483
-
484
440
  if (!lib$es6$promise$utils$$isArray(entries)) {
485
- lib$es6$promise$$internal$$reject(promise, new TypeError('You must pass an array to race.'));
486
- return promise;
487
- }
488
-
489
- var length = entries.length;
490
-
491
- function onFulfillment(value) {
492
- lib$es6$promise$$internal$$resolve(promise, value);
493
- }
494
-
495
- function onRejection(reason) {
496
- lib$es6$promise$$internal$$reject(promise, reason);
497
- }
498
-
499
- for (var i = 0; promise._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
500
- lib$es6$promise$$internal$$subscribe(Constructor.resolve(entries[i]), undefined, onFulfillment, onRejection);
441
+ return new Constructor(function(resolve, reject) {
442
+ reject(new TypeError('You must pass an array to race.'));
443
+ });
444
+ } else {
445
+ return new Constructor(function(resolve, reject) {
446
+ var length = entries.length;
447
+ for (var i = 0; i < length; i++) {
448
+ Constructor.resolve(entries[i]).then(resolve, reject);
449
+ }
450
+ });
501
451
  }
502
-
503
- return promise;
504
452
  }
505
453
  var lib$es6$promise$promise$race$$default = lib$es6$promise$promise$race$$race;
506
- function lib$es6$promise$promise$resolve$$resolve(object) {
507
- /*jshint validthis:true */
508
- var Constructor = this;
509
-
510
- if (object && typeof object === 'object' && object.constructor === Constructor) {
511
- return object;
512
- }
513
-
514
- var promise = new Constructor(lib$es6$promise$$internal$$noop);
515
- lib$es6$promise$$internal$$resolve(promise, object);
516
- return promise;
517
- }
518
- var lib$es6$promise$promise$resolve$$default = lib$es6$promise$promise$resolve$$resolve;
519
454
  function lib$es6$promise$promise$reject$$reject(reason) {
520
455
  /*jshint validthis:true */
521
456
  var Constructor = this;
@@ -525,7 +460,6 @@ if(typeof Promise != 'function'){
525
460
  }
526
461
  var lib$es6$promise$promise$reject$$default = lib$es6$promise$promise$reject$$reject;
527
462
 
528
- var lib$es6$promise$promise$$counter = 0;
529
463
 
530
464
  function lib$es6$promise$promise$$needsResolver() {
531
465
  throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
@@ -541,51 +475,66 @@ if(typeof Promise != 'function'){
541
475
  primary way of interacting with a promise is through its `then` method, which
542
476
  registers callbacks to receive either a promise's eventual value or the reason
543
477
  why the promise cannot be fulfilled.
478
+
544
479
  Terminology
545
480
  -----------
481
+
546
482
  - `promise` is an object or function with a `then` method whose behavior conforms to this specification.
547
483
  - `thenable` is an object or function that defines a `then` method.
548
484
  - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
549
485
  - `exception` is a value that is thrown using the throw statement.
550
486
  - `reason` is a value that indicates why a promise was rejected.
551
487
  - `settled` the final resting state of a promise, fulfilled or rejected.
488
+
552
489
  A promise can be in one of three states: pending, fulfilled, or rejected.
490
+
553
491
  Promises that are fulfilled have a fulfillment value and are in the fulfilled
554
492
  state. Promises that are rejected have a rejection reason and are in the
555
493
  rejected state. A fulfillment value is never a thenable.
494
+
556
495
  Promises can also be said to *resolve* a value. If this value is also a
557
496
  promise, then the original promise's settled state will match the value's
558
497
  settled state. So a promise that *resolves* a promise that rejects will
559
498
  itself reject, and a promise that *resolves* a promise that fulfills will
560
499
  itself fulfill.
500
+
501
+
561
502
  Basic Usage:
562
503
  ------------
504
+
563
505
  ```js
564
506
  var promise = new Promise(function(resolve, reject) {
565
507
  // on success
566
508
  resolve(value);
509
+
567
510
  // on failure
568
511
  reject(reason);
569
512
  });
513
+
570
514
  promise.then(function(value) {
571
515
  // on fulfillment
572
516
  }, function(reason) {
573
517
  // on rejection
574
518
  });
575
519
  ```
520
+
576
521
  Advanced Usage:
577
522
  ---------------
523
+
578
524
  Promises shine when abstracting away asynchronous interactions such as
579
525
  `XMLHttpRequest`s.
526
+
580
527
  ```js
581
528
  function getJSON(url) {
582
529
  return new Promise(function(resolve, reject){
583
530
  var xhr = new XMLHttpRequest();
531
+
584
532
  xhr.open('GET', url);
585
533
  xhr.onreadystatechange = handler;
586
534
  xhr.responseType = 'json';
587
535
  xhr.setRequestHeader('Accept', 'application/json');
588
536
  xhr.send();
537
+
589
538
  function handler() {
590
539
  if (this.readyState === this.DONE) {
591
540
  if (this.status === 200) {
@@ -597,13 +546,16 @@ if(typeof Promise != 'function'){
597
546
  };
598
547
  });
599
548
  }
549
+
600
550
  getJSON('/posts.json').then(function(json) {
601
551
  // on fulfillment
602
552
  }, function(reason) {
603
553
  // on rejection
604
554
  });
605
555
  ```
556
+
606
557
  Unlike callbacks, promises are great composable primitives.
558
+
607
559
  ```js
608
560
  Promise.all([
609
561
  getJSON('/posts'),
@@ -611,30 +563,24 @@ if(typeof Promise != 'function'){
611
563
  ]).then(function(values){
612
564
  values[0] // => postsJSON
613
565
  values[1] // => commentsJSON
566
+
614
567
  return values;
615
568
  });
616
569
  ```
570
+
617
571
  @class Promise
618
572
  @param {function} resolver
619
573
  Useful for tooling.
620
574
  @constructor
621
575
  */
622
576
  function lib$es6$promise$promise$$Promise(resolver) {
623
- this._id = lib$es6$promise$promise$$counter++;
624
- this._state = undefined;
625
- this._result = undefined;
577
+ this[lib$es6$promise$$internal$$PROMISE_ID] = lib$es6$promise$$internal$$nextId();
578
+ this._result = this._state = undefined;
626
579
  this._subscribers = [];
627
580
 
628
581
  if (lib$es6$promise$$internal$$noop !== resolver) {
629
- if (!lib$es6$promise$utils$$isFunction(resolver)) {
630
- lib$es6$promise$promise$$needsResolver();
631
- }
632
-
633
- if (!(this instanceof lib$es6$promise$promise$$Promise)) {
634
- lib$es6$promise$promise$$needsNew();
635
- }
636
-
637
- lib$es6$promise$$internal$$initializePromise(this, resolver);
582
+ typeof resolver !== 'function' && lib$es6$promise$promise$$needsResolver();
583
+ this instanceof lib$es6$promise$promise$$Promise ? lib$es6$promise$$internal$$initializePromise(this, resolver) : lib$es6$promise$promise$$needsNew();
638
584
  }
639
585
  }
640
586
 
@@ -653,6 +599,7 @@ if(typeof Promise != 'function'){
653
599
  The primary way of interacting with a promise is through its `then` method,
654
600
  which registers callbacks to receive either a promise's eventual value or the
655
601
  reason why the promise cannot be fulfilled.
602
+
656
603
  ```js
657
604
  findUser().then(function(user){
658
605
  // user is available
@@ -660,11 +607,14 @@ if(typeof Promise != 'function'){
660
607
  // user is unavailable, and you are given the reason why
661
608
  });
662
609
  ```
610
+
663
611
  Chaining
664
612
  --------
613
+
665
614
  The return value of `then` is itself a promise. This second, 'downstream'
666
615
  promise is resolved with the return value of the first promise's fulfillment
667
616
  or rejection handler, or rejected if the handler throws an exception.
617
+
668
618
  ```js
669
619
  findUser().then(function (user) {
670
620
  return user.name;
@@ -674,6 +624,7 @@ if(typeof Promise != 'function'){
674
624
  // If `findUser` fulfilled, `userName` will be the user's name, otherwise it
675
625
  // will be `'default name'`
676
626
  });
627
+
677
628
  findUser().then(function (user) {
678
629
  throw new Error('Found user, but still unhappy');
679
630
  }, function (reason) {
@@ -686,6 +637,7 @@ if(typeof Promise != 'function'){
686
637
  });
687
638
  ```
688
639
  If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
640
+
689
641
  ```js
690
642
  findUser().then(function (user) {
691
643
  throw new PedagogicalException('Upstream error');
@@ -697,12 +649,15 @@ if(typeof Promise != 'function'){
697
649
  // The `PedgagocialException` is propagated all the way down to here
698
650
  });
699
651
  ```
652
+
700
653
  Assimilation
701
654
  ------------
655
+
702
656
  Sometimes the value you want to propagate to a downstream promise can only be
703
657
  retrieved asynchronously. This can be achieved by returning a promise in the
704
658
  fulfillment or rejection handler. The downstream promise will then be pending
705
659
  until the returned promise is settled. This is called *assimilation*.
660
+
706
661
  ```js
707
662
  findUser().then(function (user) {
708
663
  return findCommentsByAuthor(user);
@@ -710,7 +665,9 @@ if(typeof Promise != 'function'){
710
665
  // The user's comments are now available
711
666
  });
712
667
  ```
668
+
713
669
  If the assimliated promise rejects, then the downstream promise will also reject.
670
+
714
671
  ```js
715
672
  findUser().then(function (user) {
716
673
  return findCommentsByAuthor(user);
@@ -720,11 +677,15 @@ if(typeof Promise != 'function'){
720
677
  // If `findCommentsByAuthor` rejects, we'll have the reason here
721
678
  });
722
679
  ```
680
+
723
681
  Simple Example
724
682
  --------------
683
+
725
684
  Synchronous Example
685
+
726
686
  ```javascript
727
687
  var result;
688
+
728
689
  try {
729
690
  result = findResult();
730
691
  // success
@@ -732,7 +693,9 @@ if(typeof Promise != 'function'){
732
693
  // failure
733
694
  }
734
695
  ```
696
+
735
697
  Errback Example
698
+
736
699
  ```js
737
700
  findResult(function(result, err){
738
701
  if (err) {
@@ -742,7 +705,9 @@ if(typeof Promise != 'function'){
742
705
  }
743
706
  });
744
707
  ```
708
+
745
709
  Promise Example;
710
+
746
711
  ```javascript
747
712
  findResult().then(function(result){
748
713
  // success
@@ -750,11 +715,15 @@ if(typeof Promise != 'function'){
750
715
  // failure
751
716
  });
752
717
  ```
718
+
753
719
  Advanced Example
754
720
  --------------
721
+
755
722
  Synchronous Example
723
+
756
724
  ```javascript
757
725
  var author, books;
726
+
758
727
  try {
759
728
  author = findAuthor();
760
729
  books = findBooksByAuthor(author);
@@ -763,12 +732,19 @@ if(typeof Promise != 'function'){
763
732
  // failure
764
733
  }
765
734
  ```
735
+
766
736
  Errback Example
737
+
767
738
  ```js
739
+
768
740
  function foundBooks(books) {
741
+
769
742
  }
743
+
770
744
  function failure(reason) {
745
+
771
746
  }
747
+
772
748
  findAuthor(function(author, err){
773
749
  if (err) {
774
750
  failure(err);
@@ -793,7 +769,9 @@ if(typeof Promise != 'function'){
793
769
  }
794
770
  });
795
771
  ```
772
+
796
773
  Promise Example;
774
+
797
775
  ```javascript
798
776
  findAuthor().
799
777
  then(findBooksByAuthor).
@@ -803,53 +781,37 @@ if(typeof Promise != 'function'){
803
781
  // something went wrong
804
782
  });
805
783
  ```
784
+
806
785
  @method then
807
786
  @param {Function} onFulfilled
808
787
  @param {Function} onRejected
809
788
  Useful for tooling.
810
789
  @return {Promise}
811
790
  */
812
- then: function(onFulfillment, onRejection) {
813
- var parent = this;
814
- var state = parent._state;
815
-
816
- if (state === lib$es6$promise$$internal$$FULFILLED && !onFulfillment || state === lib$es6$promise$$internal$$REJECTED && !onRejection) {
817
- return this;
818
- }
819
-
820
- var child = new this.constructor(lib$es6$promise$$internal$$noop);
821
- var result = parent._result;
822
-
823
- if (state) {
824
- var callback = arguments[state - 1];
825
- lib$es6$promise$asap$$asap(function(){
826
- lib$es6$promise$$internal$$invokeCallback(state, child, callback, result);
827
- });
828
- } else {
829
- lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection);
830
- }
831
-
832
- return child;
833
- },
791
+ then: lib$es6$promise$then$$default,
834
792
 
835
793
  /**
836
794
  `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
837
795
  as the catch block of a try/catch statement.
796
+
838
797
  ```js
839
798
  function findAuthor(){
840
799
  throw new Error('couldn't find that author');
841
800
  }
801
+
842
802
  // synchronous
843
803
  try {
844
804
  findAuthor();
845
805
  } catch(reason) {
846
806
  // something went wrong
847
807
  }
808
+
848
809
  // async with promises
849
810
  findAuthor().catch(function(reason){
850
811
  // something went wrong
851
812
  });
852
813
  ```
814
+
853
815
  @method catch
854
816
  @param {Function} onRejection
855
817
  Useful for tooling.
@@ -859,6 +821,101 @@ if(typeof Promise != 'function'){
859
821
  return this.then(null, onRejection);
860
822
  }
861
823
  };
824
+ var lib$es6$promise$enumerator$$default = lib$es6$promise$enumerator$$Enumerator;
825
+ function lib$es6$promise$enumerator$$Enumerator(Constructor, input) {
826
+ this._instanceConstructor = Constructor;
827
+ this.promise = new Constructor(lib$es6$promise$$internal$$noop);
828
+
829
+ if (!this.promise[lib$es6$promise$$internal$$PROMISE_ID]) {
830
+ lib$es6$promise$$internal$$makePromise(this.promise);
831
+ }
832
+
833
+ if (Array.isArray(input)) {
834
+ this._input = input;
835
+ this.length = input.length;
836
+ this._remaining = input.length;
837
+
838
+ this._result = new Array(this.length);
839
+
840
+ if (this.length === 0) {
841
+ lib$es6$promise$$internal$$fulfill(this.promise, this._result);
842
+ } else {
843
+ this.length = this.length || 0;
844
+ this._enumerate();
845
+ if (this._remaining === 0) {
846
+ lib$es6$promise$$internal$$fulfill(this.promise, this._result);
847
+ }
848
+ }
849
+ } else {
850
+ lib$es6$promise$$internal$$reject(this.promise, lib$es6$promise$enumerator$$validationError());
851
+ }
852
+ }
853
+
854
+ function lib$es6$promise$enumerator$$validationError() {
855
+ return new Error('Array Methods must be provided an Array');
856
+ }
857
+
858
+ lib$es6$promise$enumerator$$Enumerator.prototype._enumerate = function() {
859
+ var length = this.length;
860
+ var input = this._input;
861
+
862
+ for (var i = 0; this._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
863
+ this._eachEntry(input[i], i);
864
+ }
865
+ };
866
+
867
+ lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) {
868
+ var c = this._instanceConstructor;
869
+ var resolve = c.resolve;
870
+
871
+ if (resolve === lib$es6$promise$promise$resolve$$default) {
872
+ var then = lib$es6$promise$$internal$$getThen(entry);
873
+
874
+ if (then === lib$es6$promise$then$$default &&
875
+ entry._state !== lib$es6$promise$$internal$$PENDING) {
876
+ this._settledAt(entry._state, i, entry._result);
877
+ } else if (typeof then !== 'function') {
878
+ this._remaining--;
879
+ this._result[i] = entry;
880
+ } else if (c === lib$es6$promise$promise$$default) {
881
+ var promise = new c(lib$es6$promise$$internal$$noop);
882
+ lib$es6$promise$$internal$$handleMaybeThenable(promise, entry, then);
883
+ this._willSettleAt(promise, i);
884
+ } else {
885
+ this._willSettleAt(new c(function(resolve) { resolve(entry); }), i);
886
+ }
887
+ } else {
888
+ this._willSettleAt(resolve(entry), i);
889
+ }
890
+ };
891
+
892
+ lib$es6$promise$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) {
893
+ var promise = this.promise;
894
+
895
+ if (promise._state === lib$es6$promise$$internal$$PENDING) {
896
+ this._remaining--;
897
+
898
+ if (state === lib$es6$promise$$internal$$REJECTED) {
899
+ lib$es6$promise$$internal$$reject(promise, value);
900
+ } else {
901
+ this._result[i] = value;
902
+ }
903
+ }
904
+
905
+ if (this._remaining === 0) {
906
+ lib$es6$promise$$internal$$fulfill(promise, this._result);
907
+ }
908
+ };
909
+
910
+ lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) {
911
+ var enumerator = this;
912
+
913
+ lib$es6$promise$$internal$$subscribe(promise, undefined, function(value) {
914
+ enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED, i, value);
915
+ }, function(reason) {
916
+ enumerator._settledAt(lib$es6$promise$$internal$$REJECTED, i, reason);
917
+ });
918
+ };
862
919
  function lib$es6$promise$polyfill$$polyfill() {
863
920
  var local;
864
921
 
@@ -1,3 +1,3 @@
1
1
  module Es6PromisePolyfillRails
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: es6_promise_polyfill_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Greene
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-19 00:00:00.000000000 Z
11
+ date: 2016-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler