BVB Source Codes

reactide Show es6.promise.js Source code

Return Download reactide: download es6.promise.js Source code - Download reactide Source code - Type:.js
  1. 'use strict';
  2. var LIBRARY            = require('./_library')
  3.   , global             = require('./_global')
  4.   , ctx                = require('./_ctx')
  5.   , classof            = require('./_classof')
  6.   , $export            = require('./_export')
  7.   , isObject           = require('./_is-object')
  8.   , aFunction          = require('./_a-function')
  9.   , anInstance         = require('./_an-instance')
  10.   , forOf              = require('./_for-of')
  11.   , speciesConstructor = require('./_species-constructor')
  12.   , task               = require('./_task').set
  13.   , microtask          = require('./_microtask')()
  14.   , PROMISE            = 'Promise'
  15.   , TypeError          = global.TypeError
  16.   , process            = global.process
  17.   , $Promise           = global[PROMISE]
  18.   , process            = global.process
  19.   , isNode             = classof(process) == 'process'
  20.   , empty              = function(){ /* empty */ }
  21.   , Internal, GenericPromiseCapability, Wrapper;
  22.  
  23. var USE_NATIVE = !!function(){
  24.   try {
  25.     // correct subclassing with @@species support
  26.     var promise     = $Promise.resolve(1)
  27.       , FakePromise = (promise.constructor = {})[require('./_wks')('species')] = function(exec){ exec(empty, empty); };
  28.     // unhandled rejections tracking support, NodeJS Promise without it fails @@species test
  29.     return (isNode || typeof PromiseRejectionEvent == 'function') && promise.then(empty) instanceof FakePromise;
  30.   } catch(e){ /* empty */ }
  31. }();
  32.  
  33. // helpers
  34. var sameConstructor = function(a, b){
  35.   // with library wrapper special case
  36.   return a === b || a === $Promise && b === Wrapper;
  37. };
  38. var isThenable = function(it){
  39.   var then;
  40.   return isObject(it) && typeof (then = it.then) == 'function' ? then : false;
  41. };
  42. var newPromiseCapability = function(C){
  43.   return sameConstructor($Promise, C)
  44.     ? new PromiseCapability(C)
  45.     : new GenericPromiseCapability(C);
  46. };
  47. var PromiseCapability = GenericPromiseCapability = function(C){
  48.   var resolve, reject;
  49.   this.promise = new C(function($$resolve, $$reject){
  50.     if(resolve !== undefined || reject !== undefined)throw TypeError('Bad Promise constructor');
  51.     resolve = $$resolve;
  52.     reject  = $$reject;
  53.   });
  54.   this.resolve = aFunction(resolve);
  55.   this.reject  = aFunction(reject);
  56. };
  57. var perform = function(exec){
  58.   try {
  59.     exec();
  60.   } catch(e){
  61.     return {error: e};
  62.   }
  63. };
  64. var notify = function(promise, isReject){
  65.   if(promise._n)return;
  66.   promise._n = true;
  67.   var chain = promise._c;
  68.   microtask(function(){
  69.     var value = promise._v
  70.       , ok    = promise._s == 1
  71.       , i     = 0;
  72.     var run = function(reaction){
  73.       var handler = ok ? reaction.ok : reaction.fail
  74.         , resolve = reaction.resolve
  75.         , reject  = reaction.reject
  76.         , domain  = reaction.domain
  77.         , result, then;
  78.       try {
  79.         if(handler){
  80.           if(!ok){
  81.             if(promise._h == 2)onHandleUnhandled(promise);
  82.             promise._h = 1;
  83.           }
  84.           if(handler === true)result = value;
  85.           else {
  86.             if(domain)domain.enter();
  87.             result = handler(value);
  88.             if(domain)domain.exit();
  89.           }
  90.           if(result === reaction.promise){
  91.             reject(TypeError('Promise-chain cycle'));
  92.           } else if(then = isThenable(result)){
  93.             then.call(result, resolve, reject);
  94.           } else resolve(result);
  95.         } else reject(value);
  96.       } catch(e){
  97.         reject(e);
  98.       }
  99.     };
  100.     while(chain.length > i)run(chain[i++]); // variable length - can't use forEach
  101.     promise._c = [];
  102.     promise._n = false;
  103.     if(isReject && !promise._h)onUnhandled(promise);
  104.   });
  105. };
  106. var onUnhandled = function(promise){
  107.   task.call(global, function(){
  108.     var value = promise._v
  109.       , abrupt, handler, console;
  110.     if(isUnhandled(promise)){
  111.       abrupt = perform(function(){
  112.         if(isNode){
  113.           process.emit('unhandledRejection', value, promise);
  114.         } else if(handler = global.onunhandledrejection){
  115.           handler({promise: promise, reason: value});
  116.         } else if((console = global.console) && console.error){
  117.           console.error('Unhandled promise rejection', value);
  118.         }
  119.       });
  120.       // Browsers should not trigger `rejectionHandled` event if it was handled here, NodeJS - should
  121.       promise._h = isNode || isUnhandled(promise) ? 2 : 1;
  122.     } promise._a = undefined;
  123.     if(abrupt)throw abrupt.error;
  124.   });
  125. };
  126. var isUnhandled = function(promise){
  127.   if(promise._h == 1)return false;
  128.   var chain = promise._a || promise._c
  129.     , i     = 0
  130.     , reaction;
  131.   while(chain.length > i){
  132.     reaction = chain[i++];
  133.     if(reaction.fail || !isUnhandled(reaction.promise))return false;
  134.   } return true;
  135. };
  136. var onHandleUnhandled = function(promise){
  137.   task.call(global, function(){
  138.     var handler;
  139.     if(isNode){
  140.       process.emit('rejectionHandled', promise);
  141.     } else if(handler = global.onrejectionhandled){
  142.       handler({promise: promise, reason: promise._v});
  143.     }
  144.   });
  145. };
  146. var $reject = function(value){
  147.   var promise = this;
  148.   if(promise._d)return;
  149.   promise._d = true;
  150.   promise = promise._w || promise; // unwrap
  151.   promise._v = value;
  152.   promise._s = 2;
  153.   if(!promise._a)promise._a = promise._c.slice();
  154.   notify(promise, true);
  155. };
  156. var $resolve = function(value){
  157.   var promise = this
  158.     , then;
  159.   if(promise._d)return;
  160.   promise._d = true;
  161.   promise = promise._w || promise; // unwrap
  162.   try {
  163.     if(promise === value)throw TypeError("Promise can't be resolved itself");
  164.     if(then = isThenable(value)){
  165.       microtask(function(){
  166.         var wrapper = {_w: promise, _d: false}; // wrap
  167.         try {
  168.           then.call(value, ctx($resolve, wrapper, 1), ctx($reject, wrapper, 1));
  169.         } catch(e){
  170.           $reject.call(wrapper, e);
  171.         }
  172.       });
  173.     } else {
  174.       promise._v = value;
  175.       promise._s = 1;
  176.       notify(promise, false);
  177.     }
  178.   } catch(e){
  179.     $reject.call({_w: promise, _d: false}, e); // wrap
  180.   }
  181. };
  182.  
  183. // constructor polyfill
  184. if(!USE_NATIVE){
  185.   // 25.4.3.1 Promise(executor)
  186.   $Promise = function Promise(executor){
  187.     anInstance(this, $Promise, PROMISE, '_h');
  188.     aFunction(executor);
  189.     Internal.call(this);
  190.     try {
  191.       executor(ctx($resolve, this, 1), ctx($reject, this, 1));
  192.     } catch(err){
  193.       $reject.call(this, err);
  194.     }
  195.   };
  196.   Internal = function Promise(executor){
  197.     this._c = [];             // <- awaiting reactions
  198.     this._a = undefined;      // <- checked in isUnhandled reactions
  199.     this._s = 0;              // <- state
  200.     this._d = false;          // <- done
  201.     this._v = undefined;      // <- value
  202.     this._h = 0;              // <- rejection state, 0 - default, 1 - handled, 2 - unhandled
  203.     this._n = false;          // <- notify
  204.   };
  205.   Internal.prototype = require('./_redefine-all')($Promise.prototype, {
  206.     // 25.4.5.3 Promise.prototype.then(onFulfilled, onRejected)
  207.     then: function then(onFulfilled, onRejected){
  208.       var reaction    = newPromiseCapability(speciesConstructor(this, $Promise));
  209.       reaction.ok     = typeof onFulfilled == 'function' ? onFulfilled : true;
  210.       reaction.fail   = typeof onRejected == 'function' && onRejected;
  211.       reaction.domain = isNode ? process.domain : undefined;
  212.       this._c.push(reaction);
  213.       if(this._a)this._a.push(reaction);
  214.       if(this._s)notify(this, false);
  215.       return reaction.promise;
  216.     },
  217.     // 25.4.5.1 Promise.prototype.catch(onRejected)
  218.     'catch': function(onRejected){
  219.       return this.then(undefined, onRejected);
  220.     }
  221.   });
  222.   PromiseCapability = function(){
  223.     var promise  = new Internal;
  224.     this.promise = promise;
  225.     this.resolve = ctx($resolve, promise, 1);
  226.     this.reject  = ctx($reject, promise, 1);
  227.   };
  228. }
  229.  
  230. $export($export.G + $export.W + $export.F * !USE_NATIVE, {Promise: $Promise});
  231. require('./_set-to-string-tag')($Promise, PROMISE);
  232. require('./_set-species')(PROMISE);
  233. Wrapper = require('./_core')[PROMISE];
  234.  
  235. // statics
  236. $export($export.S + $export.F * !USE_NATIVE, PROMISE, {
  237.   // 25.4.4.5 Promise.reject(r)
  238.   reject: function reject(r){
  239.     var capability = newPromiseCapability(this)
  240.       , $$reject   = capability.reject;
  241.     $$reject(r);
  242.     return capability.promise;
  243.   }
  244. });
  245. $export($export.S + $export.F * (LIBRARY || !USE_NATIVE), PROMISE, {
  246.   // 25.4.4.6 Promise.resolve(x)
  247.   resolve: function resolve(x){
  248.     // instanceof instead of internal slot check because we should fix it without replacement native Promise core
  249.     if(x instanceof $Promise && sameConstructor(x.constructor, this))return x;
  250.     var capability = newPromiseCapability(this)
  251.       , $$resolve  = capability.resolve;
  252.     $$resolve(x);
  253.     return capability.promise;
  254.   }
  255. });
  256. $export($export.S + $export.F * !(USE_NATIVE && require('./_iter-detect')(function(iter){
  257.   $Promise.all(iter)['catch'](empty);
  258. })), PROMISE, {
  259.   // 25.4.4.1 Promise.all(iterable)
  260.   all: function all(iterable){
  261.     var C          = this
  262.       , capability = newPromiseCapability(C)
  263.       , resolve    = capability.resolve
  264.       , reject     = capability.reject;
  265.     var abrupt = perform(function(){
  266.       var values    = []
  267.         , index     = 0
  268.         , remaining = 1;
  269.       forOf(iterable, false, function(promise){
  270.         var $index        = index++
  271.           , alreadyCalled = false;
  272.         values.push(undefined);
  273.         remaining++;
  274.         C.resolve(promise).then(function(value){
  275.           if(alreadyCalled)return;
  276.           alreadyCalled  = true;
  277.           values[$index] = value;
  278.           --remaining || resolve(values);
  279.         }, reject);
  280.       });
  281.       --remaining || resolve(values);
  282.     });
  283.     if(abrupt)reject(abrupt.error);
  284.     return capability.promise;
  285.   },
  286.   // 25.4.4.4 Promise.race(iterable)
  287.   race: function race(iterable){
  288.     var C          = this
  289.       , capability = newPromiseCapability(C)
  290.       , reject     = capability.reject;
  291.     var abrupt = perform(function(){
  292.       forOf(iterable, false, function(promise){
  293.         C.resolve(promise).then(capability.resolve, reject);
  294.       });
  295.     });
  296.     if(abrupt)reject(abrupt.error);
  297.     return capability.promise;
  298.   }
  299. });
downloades6.promise.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top