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')()
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;
23 var USE_NATIVE = !!function(){
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 */ }
34 var sameConstructor = function(a, b){
35 // with library wrapper special case
36 return a === b || a === $Promise && b === Wrapper;
38 var isThenable = function(it){
40 return isObject(it) && typeof (then = it.then) == 'function' ? then : false;
42 var newPromiseCapability = function(C){
43 return sameConstructor($Promise, C)
44 ? new PromiseCapability(C)
45 : new GenericPromiseCapability(C);
47 var PromiseCapability = GenericPromiseCapability = function(C){
49 this.promise = new C(function($$resolve, $$reject){
50 if(resolve !== undefined || reject !== undefined)throw TypeError('Bad Promise constructor');
54 this.resolve = aFunction(resolve);
55 this.reject = aFunction(reject);
57 var perform = function(exec){
64 var notify = function(promise, isReject){
67 var chain = promise._c;
69 var value = promise._v
70 , ok = promise._s == 1
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
81 if(promise._h == 2)onHandleUnhandled(promise);
84 if(handler === true)result = value;
86 if(domain)domain.enter();
87 result = handler(value);
88 if(domain)domain.exit();
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);
100 while(chain.length > i)run(chain[i++]); // variable length - can't use forEach
103 if(isReject && !promise._h)onUnhandled(promise);
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(){
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);
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;
126 var isUnhandled = function(promise){
127 if(promise._h == 1)return false;
128 var chain = promise._a || promise._c
131 while(chain.length > i){
132 reaction = chain[i++];
133 if(reaction.fail || !isUnhandled(reaction.promise))return false;
136 var onHandleUnhandled = function(promise){
137 task.call(global, function(){
140 process.emit('rejectionHandled', promise);
141 } else if(handler = global.onrejectionhandled){
142 handler({promise: promise, reason: promise._v});
146 var $reject = function(value){
148 if(promise._d)return;
150 promise = promise._w || promise; // unwrap
153 if(!promise._a)promise._a = promise._c.slice();
154 notify(promise, true);
156 var $resolve = function(value){
159 if(promise._d)return;
161 promise = promise._w || promise; // unwrap
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
168 then.call(value, ctx($resolve, wrapper, 1), ctx($reject, wrapper, 1));
170 $reject.call(wrapper, e);
176 notify(promise, false);
179 $reject.call({_w: promise, _d: false}, e); // wrap
183 // constructor polyfill
185 // 25.4.3.1 Promise(executor)
186 $Promise = function Promise(executor){
187 anInstance(this, $Promise, PROMISE, '_h');
191 executor(ctx($resolve, this, 1), ctx($reject, this, 1));
193 $reject.call(this, err);
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
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;
217 // 25.4.5.1 Promise.prototype.catch(onRejected)
218 'catch': function(onRejected){
219 return this.then(undefined, onRejected);
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);
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];
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;
242 return capability.promise;
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;
253 return capability.promise;
256 $export($export.S + $export.F * !(USE_NATIVE && require('./_iter-detect')(function(iter){
257 $Promise.all(iter)['catch'](empty);
259 // 25.4.4.1 Promise.all(iterable)
260 all: function all(iterable){
262 , capability = newPromiseCapability(C)
263 , resolve = capability.resolve
264 , reject = capability.reject;
265 var abrupt = perform(function(){
269 forOf(iterable, false, function(promise){
271 , alreadyCalled = false;
272 values.push(undefined);
274 C.resolve(promise).then(function(value){
275 if(alreadyCalled)return;
276 alreadyCalled = true;
277 values[$index] = value;
278 --remaining || resolve(values);
281 --remaining || resolve(values);
283 if(abrupt)reject(abrupt.error);
284 return capability.promise;
286 // 25.4.4.4 Promise.race(iterable)
287 race: function race(iterable){
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);
296 if(abrupt)reject(abrupt.error);
297 return capability.promise;