source: trip-planner-front/node_modules/core-js/modules/es.promise.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 14.0 KB
Line 
1'use strict';
2var $ = require('../internals/export');
3var IS_PURE = require('../internals/is-pure');
4var global = require('../internals/global');
5var getBuiltIn = require('../internals/get-built-in');
6var NativePromise = require('../internals/native-promise-constructor');
7var redefine = require('../internals/redefine');
8var redefineAll = require('../internals/redefine-all');
9var setPrototypeOf = require('../internals/object-set-prototype-of');
10var setToStringTag = require('../internals/set-to-string-tag');
11var setSpecies = require('../internals/set-species');
12var isObject = require('../internals/is-object');
13var aFunction = require('../internals/a-function');
14var anInstance = require('../internals/an-instance');
15var inspectSource = require('../internals/inspect-source');
16var iterate = require('../internals/iterate');
17var checkCorrectnessOfIteration = require('../internals/check-correctness-of-iteration');
18var speciesConstructor = require('../internals/species-constructor');
19var task = require('../internals/task').set;
20var microtask = require('../internals/microtask');
21var promiseResolve = require('../internals/promise-resolve');
22var hostReportErrors = require('../internals/host-report-errors');
23var newPromiseCapabilityModule = require('../internals/new-promise-capability');
24var perform = require('../internals/perform');
25var InternalStateModule = require('../internals/internal-state');
26var isForced = require('../internals/is-forced');
27var wellKnownSymbol = require('../internals/well-known-symbol');
28var IS_BROWSER = require('../internals/engine-is-browser');
29var IS_NODE = require('../internals/engine-is-node');
30var V8_VERSION = require('../internals/engine-v8-version');
31
32var SPECIES = wellKnownSymbol('species');
33var PROMISE = 'Promise';
34var getInternalState = InternalStateModule.get;
35var setInternalState = InternalStateModule.set;
36var getInternalPromiseState = InternalStateModule.getterFor(PROMISE);
37var NativePromisePrototype = NativePromise && NativePromise.prototype;
38var PromiseConstructor = NativePromise;
39var PromiseConstructorPrototype = NativePromisePrototype;
40var TypeError = global.TypeError;
41var document = global.document;
42var process = global.process;
43var newPromiseCapability = newPromiseCapabilityModule.f;
44var newGenericPromiseCapability = newPromiseCapability;
45var DISPATCH_EVENT = !!(document && document.createEvent && global.dispatchEvent);
46var NATIVE_REJECTION_EVENT = typeof PromiseRejectionEvent == 'function';
47var UNHANDLED_REJECTION = 'unhandledrejection';
48var REJECTION_HANDLED = 'rejectionhandled';
49var PENDING = 0;
50var FULFILLED = 1;
51var REJECTED = 2;
52var HANDLED = 1;
53var UNHANDLED = 2;
54var SUBCLASSING = false;
55var Internal, OwnPromiseCapability, PromiseWrapper, nativeThen;
56
57var FORCED = isForced(PROMISE, function () {
58 var PROMISE_CONSTRUCTOR_SOURCE = inspectSource(PromiseConstructor);
59 var GLOBAL_CORE_JS_PROMISE = PROMISE_CONSTRUCTOR_SOURCE !== String(PromiseConstructor);
60 // V8 6.6 (Node 10 and Chrome 66) have a bug with resolving custom thenables
61 // https://bugs.chromium.org/p/chromium/issues/detail?id=830565
62 // We can't detect it synchronously, so just check versions
63 if (!GLOBAL_CORE_JS_PROMISE && V8_VERSION === 66) return true;
64 // We need Promise#finally in the pure version for preventing prototype pollution
65 if (IS_PURE && !PromiseConstructorPrototype['finally']) return true;
66 // We can't use @@species feature detection in V8 since it causes
67 // deoptimization and performance degradation
68 // https://github.com/zloirock/core-js/issues/679
69 if (V8_VERSION >= 51 && /native code/.test(PROMISE_CONSTRUCTOR_SOURCE)) return false;
70 // Detect correctness of subclassing with @@species support
71 var promise = new PromiseConstructor(function (resolve) { resolve(1); });
72 var FakePromise = function (exec) {
73 exec(function () { /* empty */ }, function () { /* empty */ });
74 };
75 var constructor = promise.constructor = {};
76 constructor[SPECIES] = FakePromise;
77 SUBCLASSING = promise.then(function () { /* empty */ }) instanceof FakePromise;
78 if (!SUBCLASSING) return true;
79 // Unhandled rejections tracking support, NodeJS Promise without it fails @@species test
80 return !GLOBAL_CORE_JS_PROMISE && IS_BROWSER && !NATIVE_REJECTION_EVENT;
81});
82
83var INCORRECT_ITERATION = FORCED || !checkCorrectnessOfIteration(function (iterable) {
84 PromiseConstructor.all(iterable)['catch'](function () { /* empty */ });
85});
86
87// helpers
88var isThenable = function (it) {
89 var then;
90 return isObject(it) && typeof (then = it.then) == 'function' ? then : false;
91};
92
93var notify = function (state, isReject) {
94 if (state.notified) return;
95 state.notified = true;
96 var chain = state.reactions;
97 microtask(function () {
98 var value = state.value;
99 var ok = state.state == FULFILLED;
100 var index = 0;
101 // variable length - can't use forEach
102 while (chain.length > index) {
103 var reaction = chain[index++];
104 var handler = ok ? reaction.ok : reaction.fail;
105 var resolve = reaction.resolve;
106 var reject = reaction.reject;
107 var domain = reaction.domain;
108 var result, then, exited;
109 try {
110 if (handler) {
111 if (!ok) {
112 if (state.rejection === UNHANDLED) onHandleUnhandled(state);
113 state.rejection = HANDLED;
114 }
115 if (handler === true) result = value;
116 else {
117 if (domain) domain.enter();
118 result = handler(value); // can throw
119 if (domain) {
120 domain.exit();
121 exited = true;
122 }
123 }
124 if (result === reaction.promise) {
125 reject(TypeError('Promise-chain cycle'));
126 } else if (then = isThenable(result)) {
127 then.call(result, resolve, reject);
128 } else resolve(result);
129 } else reject(value);
130 } catch (error) {
131 if (domain && !exited) domain.exit();
132 reject(error);
133 }
134 }
135 state.reactions = [];
136 state.notified = false;
137 if (isReject && !state.rejection) onUnhandled(state);
138 });
139};
140
141var dispatchEvent = function (name, promise, reason) {
142 var event, handler;
143 if (DISPATCH_EVENT) {
144 event = document.createEvent('Event');
145 event.promise = promise;
146 event.reason = reason;
147 event.initEvent(name, false, true);
148 global.dispatchEvent(event);
149 } else event = { promise: promise, reason: reason };
150 if (!NATIVE_REJECTION_EVENT && (handler = global['on' + name])) handler(event);
151 else if (name === UNHANDLED_REJECTION) hostReportErrors('Unhandled promise rejection', reason);
152};
153
154var onUnhandled = function (state) {
155 task.call(global, function () {
156 var promise = state.facade;
157 var value = state.value;
158 var IS_UNHANDLED = isUnhandled(state);
159 var result;
160 if (IS_UNHANDLED) {
161 result = perform(function () {
162 if (IS_NODE) {
163 process.emit('unhandledRejection', value, promise);
164 } else dispatchEvent(UNHANDLED_REJECTION, promise, value);
165 });
166 // Browsers should not trigger `rejectionHandled` event if it was handled here, NodeJS - should
167 state.rejection = IS_NODE || isUnhandled(state) ? UNHANDLED : HANDLED;
168 if (result.error) throw result.value;
169 }
170 });
171};
172
173var isUnhandled = function (state) {
174 return state.rejection !== HANDLED && !state.parent;
175};
176
177var onHandleUnhandled = function (state) {
178 task.call(global, function () {
179 var promise = state.facade;
180 if (IS_NODE) {
181 process.emit('rejectionHandled', promise);
182 } else dispatchEvent(REJECTION_HANDLED, promise, state.value);
183 });
184};
185
186var bind = function (fn, state, unwrap) {
187 return function (value) {
188 fn(state, value, unwrap);
189 };
190};
191
192var internalReject = function (state, value, unwrap) {
193 if (state.done) return;
194 state.done = true;
195 if (unwrap) state = unwrap;
196 state.value = value;
197 state.state = REJECTED;
198 notify(state, true);
199};
200
201var internalResolve = function (state, value, unwrap) {
202 if (state.done) return;
203 state.done = true;
204 if (unwrap) state = unwrap;
205 try {
206 if (state.facade === value) throw TypeError("Promise can't be resolved itself");
207 var then = isThenable(value);
208 if (then) {
209 microtask(function () {
210 var wrapper = { done: false };
211 try {
212 then.call(value,
213 bind(internalResolve, wrapper, state),
214 bind(internalReject, wrapper, state)
215 );
216 } catch (error) {
217 internalReject(wrapper, error, state);
218 }
219 });
220 } else {
221 state.value = value;
222 state.state = FULFILLED;
223 notify(state, false);
224 }
225 } catch (error) {
226 internalReject({ done: false }, error, state);
227 }
228};
229
230// constructor polyfill
231if (FORCED) {
232 // 25.4.3.1 Promise(executor)
233 PromiseConstructor = function Promise(executor) {
234 anInstance(this, PromiseConstructor, PROMISE);
235 aFunction(executor);
236 Internal.call(this);
237 var state = getInternalState(this);
238 try {
239 executor(bind(internalResolve, state), bind(internalReject, state));
240 } catch (error) {
241 internalReject(state, error);
242 }
243 };
244 PromiseConstructorPrototype = PromiseConstructor.prototype;
245 // eslint-disable-next-line no-unused-vars -- required for `.length`
246 Internal = function Promise(executor) {
247 setInternalState(this, {
248 type: PROMISE,
249 done: false,
250 notified: false,
251 parent: false,
252 reactions: [],
253 rejection: false,
254 state: PENDING,
255 value: undefined
256 });
257 };
258 Internal.prototype = redefineAll(PromiseConstructorPrototype, {
259 // `Promise.prototype.then` method
260 // https://tc39.es/ecma262/#sec-promise.prototype.then
261 then: function then(onFulfilled, onRejected) {
262 var state = getInternalPromiseState(this);
263 var reaction = newPromiseCapability(speciesConstructor(this, PromiseConstructor));
264 reaction.ok = typeof onFulfilled == 'function' ? onFulfilled : true;
265 reaction.fail = typeof onRejected == 'function' && onRejected;
266 reaction.domain = IS_NODE ? process.domain : undefined;
267 state.parent = true;
268 state.reactions.push(reaction);
269 if (state.state != PENDING) notify(state, false);
270 return reaction.promise;
271 },
272 // `Promise.prototype.catch` method
273 // https://tc39.es/ecma262/#sec-promise.prototype.catch
274 'catch': function (onRejected) {
275 return this.then(undefined, onRejected);
276 }
277 });
278 OwnPromiseCapability = function () {
279 var promise = new Internal();
280 var state = getInternalState(promise);
281 this.promise = promise;
282 this.resolve = bind(internalResolve, state);
283 this.reject = bind(internalReject, state);
284 };
285 newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
286 return C === PromiseConstructor || C === PromiseWrapper
287 ? new OwnPromiseCapability(C)
288 : newGenericPromiseCapability(C);
289 };
290
291 if (!IS_PURE && typeof NativePromise == 'function' && NativePromisePrototype !== Object.prototype) {
292 nativeThen = NativePromisePrototype.then;
293
294 if (!SUBCLASSING) {
295 // make `Promise#then` return a polyfilled `Promise` for native promise-based APIs
296 redefine(NativePromisePrototype, 'then', function then(onFulfilled, onRejected) {
297 var that = this;
298 return new PromiseConstructor(function (resolve, reject) {
299 nativeThen.call(that, resolve, reject);
300 }).then(onFulfilled, onRejected);
301 // https://github.com/zloirock/core-js/issues/640
302 }, { unsafe: true });
303
304 // makes sure that native promise-based APIs `Promise#catch` properly works with patched `Promise#then`
305 redefine(NativePromisePrototype, 'catch', PromiseConstructorPrototype['catch'], { unsafe: true });
306 }
307
308 // make `.constructor === Promise` work for native promise-based APIs
309 try {
310 delete NativePromisePrototype.constructor;
311 } catch (error) { /* empty */ }
312
313 // make `instanceof Promise` work for native promise-based APIs
314 if (setPrototypeOf) {
315 setPrototypeOf(NativePromisePrototype, PromiseConstructorPrototype);
316 }
317 }
318}
319
320$({ global: true, wrap: true, forced: FORCED }, {
321 Promise: PromiseConstructor
322});
323
324setToStringTag(PromiseConstructor, PROMISE, false, true);
325setSpecies(PROMISE);
326
327PromiseWrapper = getBuiltIn(PROMISE);
328
329// statics
330$({ target: PROMISE, stat: true, forced: FORCED }, {
331 // `Promise.reject` method
332 // https://tc39.es/ecma262/#sec-promise.reject
333 reject: function reject(r) {
334 var capability = newPromiseCapability(this);
335 capability.reject.call(undefined, r);
336 return capability.promise;
337 }
338});
339
340$({ target: PROMISE, stat: true, forced: IS_PURE || FORCED }, {
341 // `Promise.resolve` method
342 // https://tc39.es/ecma262/#sec-promise.resolve
343 resolve: function resolve(x) {
344 return promiseResolve(IS_PURE && this === PromiseWrapper ? PromiseConstructor : this, x);
345 }
346});
347
348$({ target: PROMISE, stat: true, forced: INCORRECT_ITERATION }, {
349 // `Promise.all` method
350 // https://tc39.es/ecma262/#sec-promise.all
351 all: function all(iterable) {
352 var C = this;
353 var capability = newPromiseCapability(C);
354 var resolve = capability.resolve;
355 var reject = capability.reject;
356 var result = perform(function () {
357 var $promiseResolve = aFunction(C.resolve);
358 var values = [];
359 var counter = 0;
360 var remaining = 1;
361 iterate(iterable, function (promise) {
362 var index = counter++;
363 var alreadyCalled = false;
364 values.push(undefined);
365 remaining++;
366 $promiseResolve.call(C, promise).then(function (value) {
367 if (alreadyCalled) return;
368 alreadyCalled = true;
369 values[index] = value;
370 --remaining || resolve(values);
371 }, reject);
372 });
373 --remaining || resolve(values);
374 });
375 if (result.error) reject(result.value);
376 return capability.promise;
377 },
378 // `Promise.race` method
379 // https://tc39.es/ecma262/#sec-promise.race
380 race: function race(iterable) {
381 var C = this;
382 var capability = newPromiseCapability(C);
383 var reject = capability.reject;
384 var result = perform(function () {
385 var $promiseResolve = aFunction(C.resolve);
386 iterate(iterable, function (promise) {
387 $promiseResolve.call(C, promise).then(capability.resolve, reject);
388 });
389 });
390 if (result.error) reject(result.value);
391 return capability.promise;
392 }
393});
Note: See TracBrowser for help on using the repository browser.