es6.promise.js 9.8 KB

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