source: frontend/node_modules/core-js-pure/modules/es.promise.constructor.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 9.7 KB
Line 
1'use strict';
2var $ = require('../internals/export');
3var IS_PURE = require('../internals/is-pure');
4var IS_NODE = require('../internals/environment-is-node');
5var globalThis = require('../internals/global-this');
6var path = require('../internals/path');
7var call = require('../internals/function-call');
8var defineBuiltIn = require('../internals/define-built-in');
9var setPrototypeOf = require('../internals/object-set-prototype-of');
10var setToStringTag = require('../internals/set-to-string-tag');
11var setSpecies = require('../internals/set-species');
12var aCallable = require('../internals/a-callable');
13var isCallable = require('../internals/is-callable');
14var isObject = require('../internals/is-object');
15var anInstance = require('../internals/an-instance');
16var speciesConstructor = require('../internals/species-constructor');
17var task = require('../internals/task').set;
18var microtask = require('../internals/microtask');
19var hostReportErrors = require('../internals/host-report-errors');
20var perform = require('../internals/perform');
21var Queue = require('../internals/queue');
22var InternalStateModule = require('../internals/internal-state');
23var NativePromiseConstructor = require('../internals/promise-native-constructor');
24var PromiseConstructorDetection = require('../internals/promise-constructor-detection');
25var newPromiseCapabilityModule = require('../internals/new-promise-capability');
26
27var PROMISE = 'Promise';
28var FORCED_PROMISE_CONSTRUCTOR = PromiseConstructorDetection.CONSTRUCTOR;
29var NATIVE_PROMISE_REJECTION_EVENT = PromiseConstructorDetection.REJECTION_EVENT;
30var NATIVE_PROMISE_SUBCLASSING = PromiseConstructorDetection.SUBCLASSING;
31var getInternalPromiseState = InternalStateModule.getterFor(PROMISE);
32var setInternalState = InternalStateModule.set;
33var NativePromisePrototype = NativePromiseConstructor && NativePromiseConstructor.prototype;
34var PromiseConstructor = NativePromiseConstructor;
35var PromisePrototype = NativePromisePrototype;
36var TypeError = globalThis.TypeError;
37var document = globalThis.document;
38var process = globalThis.process;
39var newPromiseCapability = newPromiseCapabilityModule.f;
40var newGenericPromiseCapability = newPromiseCapability;
41
42var DISPATCH_EVENT = !!(document && document.createEvent && globalThis.dispatchEvent);
43var UNHANDLED_REJECTION = 'unhandledrejection';
44var REJECTION_HANDLED = 'rejectionhandled';
45var PENDING = 0;
46var FULFILLED = 1;
47var REJECTED = 2;
48var HANDLED = 1;
49var UNHANDLED = 2;
50
51var Internal, OwnPromiseCapability, PromiseWrapper, nativeThen;
52
53// helpers
54var isThenable = function (it) {
55 var then;
56 return isObject(it) && isCallable(then = it.then) ? then : false;
57};
58
59var callReaction = function (reaction, state) {
60 var value = state.value;
61 var ok = state.state === FULFILLED;
62 var handler = ok ? reaction.ok : reaction.fail;
63 var resolve = reaction.resolve;
64 var reject = reaction.reject;
65 var domain = reaction.domain;
66 var result, then, exited;
67 try {
68 if (handler) {
69 if (!ok) {
70 if (state.rejection === UNHANDLED) onHandleUnhandled(state);
71 state.rejection = HANDLED;
72 }
73 if (handler === true) result = value;
74 else {
75 if (domain) domain.enter();
76 result = handler(value); // can throw
77 if (domain) {
78 domain.exit();
79 exited = true;
80 }
81 }
82 if (result === reaction.promise) {
83 reject(new TypeError('Promise-chain cycle'));
84 } else if (then = isThenable(result)) {
85 call(then, result, resolve, reject);
86 } else resolve(result);
87 } else reject(value);
88 } catch (error) {
89 if (domain && !exited) domain.exit();
90 reject(error);
91 }
92};
93
94var notify = function (state, isReject) {
95 if (state.notified) return;
96 state.notified = true;
97 microtask(function () {
98 var reactions = state.reactions;
99 var reaction;
100 while (reaction = reactions.get()) {
101 callReaction(reaction, state);
102 }
103 state.notified = false;
104 if (isReject && !state.rejection) onUnhandled(state);
105 });
106};
107
108var dispatchEvent = function (name, promise, reason) {
109 var event, handler;
110 if (DISPATCH_EVENT) {
111 event = document.createEvent('Event');
112 event.promise = promise;
113 event.reason = reason;
114 event.initEvent(name, false, true);
115 globalThis.dispatchEvent(event);
116 } else event = { promise: promise, reason: reason };
117 if (!NATIVE_PROMISE_REJECTION_EVENT && (handler = globalThis['on' + name])) handler(event);
118 else if (name === UNHANDLED_REJECTION) hostReportErrors('Unhandled promise rejection', reason);
119};
120
121var onUnhandled = function (state) {
122 call(task, globalThis, function () {
123 var promise = state.facade;
124 var value = state.value;
125 var IS_UNHANDLED = isUnhandled(state);
126 var result;
127 if (IS_UNHANDLED) {
128 result = perform(function () {
129 if (IS_NODE) {
130 process.emit('unhandledRejection', value, promise);
131 } else dispatchEvent(UNHANDLED_REJECTION, promise, value);
132 });
133 // Browsers should not trigger `rejectionHandled` event if it was handled here, NodeJS - should
134 state.rejection = IS_NODE || isUnhandled(state) ? UNHANDLED : HANDLED;
135 if (result.error) throw result.value;
136 }
137 });
138};
139
140var isUnhandled = function (state) {
141 return state.rejection !== HANDLED && !state.parent;
142};
143
144var onHandleUnhandled = function (state) {
145 call(task, globalThis, function () {
146 var promise = state.facade;
147 if (IS_NODE) {
148 process.emit('rejectionHandled', promise);
149 } else dispatchEvent(REJECTION_HANDLED, promise, state.value);
150 });
151};
152
153var bind = function (fn, state, unwrap) {
154 return function (value) {
155 fn(state, value, unwrap);
156 };
157};
158
159var internalReject = function (state, value, unwrap) {
160 if (state.done) return;
161 state.done = true;
162 if (unwrap) state = unwrap;
163 state.value = value;
164 state.state = REJECTED;
165 notify(state, true);
166};
167
168var internalResolve = function (state, value, unwrap) {
169 if (state.done) return;
170 state.done = true;
171 if (unwrap) state = unwrap;
172 try {
173 if (state.facade === value) throw new TypeError("Promise can't be resolved itself");
174 var then = isThenable(value);
175 if (then) {
176 microtask(function () {
177 var wrapper = { done: false };
178 try {
179 call(then, value,
180 bind(internalResolve, wrapper, state),
181 bind(internalReject, wrapper, state)
182 );
183 } catch (error) {
184 internalReject(wrapper, error, state);
185 }
186 });
187 } else {
188 state.value = value;
189 state.state = FULFILLED;
190 notify(state, false);
191 }
192 } catch (error) {
193 internalReject({ done: false }, error, state);
194 }
195};
196
197// constructor polyfill
198if (FORCED_PROMISE_CONSTRUCTOR) {
199 // 25.4.3.1 Promise(executor)
200 PromiseConstructor = function Promise(executor) {
201 anInstance(this, PromisePrototype);
202 aCallable(executor);
203 call(Internal, this);
204 var state = getInternalPromiseState(this);
205 try {
206 executor(bind(internalResolve, state), bind(internalReject, state));
207 } catch (error) {
208 internalReject(state, error);
209 }
210 };
211
212 PromisePrototype = PromiseConstructor.prototype;
213
214 // eslint-disable-next-line no-unused-vars -- required for `.length`
215 Internal = function Promise(executor) {
216 setInternalState(this, {
217 type: PROMISE,
218 done: false,
219 notified: false,
220 parent: false,
221 reactions: new Queue(),
222 rejection: false,
223 state: PENDING,
224 value: null
225 });
226 };
227
228 // `Promise.prototype.then` method
229 // https://tc39.es/ecma262/#sec-promise.prototype.then
230 Internal.prototype = defineBuiltIn(PromisePrototype, 'then', function then(onFulfilled, onRejected) {
231 var state = getInternalPromiseState(this);
232 var reaction = newPromiseCapability(speciesConstructor(this, PromiseConstructor));
233 state.parent = true;
234 reaction.ok = isCallable(onFulfilled) ? onFulfilled : true;
235 reaction.fail = isCallable(onRejected) && onRejected;
236 reaction.domain = IS_NODE ? process.domain : undefined;
237 if (state.state === PENDING) state.reactions.add(reaction);
238 else microtask(function () {
239 callReaction(reaction, state);
240 });
241 return reaction.promise;
242 });
243
244 OwnPromiseCapability = function () {
245 var promise = new Internal();
246 var state = getInternalPromiseState(promise);
247 this.promise = promise;
248 this.resolve = bind(internalResolve, state);
249 this.reject = bind(internalReject, state);
250 };
251
252 newPromiseCapabilityModule.f = newPromiseCapability = function (C) {
253 return C === PromiseConstructor || C === PromiseWrapper
254 ? new OwnPromiseCapability(C)
255 : newGenericPromiseCapability(C);
256 };
257
258 if (!IS_PURE && isCallable(NativePromiseConstructor) && NativePromisePrototype !== Object.prototype) {
259 nativeThen = NativePromisePrototype.then;
260
261 if (!NATIVE_PROMISE_SUBCLASSING) {
262 // make `Promise#then` return a polyfilled `Promise` for native promise-based APIs
263 defineBuiltIn(NativePromisePrototype, 'then', function then(onFulfilled, onRejected) {
264 var that = this;
265 return new PromiseConstructor(function (resolve, reject) {
266 call(nativeThen, that, resolve, reject);
267 }).then(onFulfilled, onRejected);
268 // https://github.com/zloirock/core-js/issues/640
269 }, { unsafe: true });
270 }
271
272 // make `.constructor === Promise` work for native promise-based APIs
273 try {
274 delete NativePromisePrototype.constructor;
275 } catch (error) { /* empty */ }
276
277 // make `instanceof Promise` work for native promise-based APIs
278 if (setPrototypeOf) {
279 setPrototypeOf(NativePromisePrototype, PromisePrototype);
280 }
281 }
282}
283
284// `Promise` constructor
285// https://tc39.es/ecma262/#sec-promise-executor
286$({ global: true, constructor: true, wrap: true, forced: FORCED_PROMISE_CONSTRUCTOR }, {
287 Promise: PromiseConstructor
288});
289
290PromiseWrapper = path.Promise;
291
292setToStringTag(PromiseConstructor, PROMISE, false, true);
293setSpecies(PROMISE);
Note: See TracBrowser for help on using the repository browser.