1; /* /bitrix/js/main/polyfill/promise/js/promise.js?16237146328241*/ 2 3; /* Start:"a:4:{s:4:"full";s:61:"/bitrix/js/main/polyfill/promise/js/promise.js?16237146328241";s:6:"source";s:46:"/bitrix/js/main/polyfill/promise/js/promise.js";s:3:"min";s:0:"";s:3:"map";s:0:"";}"*/ 4;(function(window) { 5 'use strict'; 6 7 if (typeof window.Promise === 'undefined' || 8 window.Promise.toString().indexOf('[native code]') === -1) 9 { 10 var PROMISE_STATUS = '[[PromiseStatus]]'; 11 var PROMISE_VALUE = '[[PromiseValue]]'; 12 var STATUS_PENDING = 'pending'; 13 var STATUS_INTERNAL_PENDING = 'internal pending'; 14 var STATUS_RESOLVED = 'resolved'; 15 var STATUS_REJECTED = 'rejected'; 16 17 18 /** 19 * Handles promise done 20 * @param {Promise} promise 21 * @param {Handler} deferred 22 */ 23 var handle = function(promise, deferred) 24 { 25 if (promise[PROMISE_STATUS] === STATUS_INTERNAL_PENDING) 26 { 27 promise = promise[PROMISE_VALUE]; 28 } 29 30 if (promise[PROMISE_STATUS] === STATUS_PENDING) 31 { 32 promise.deferreds.push(deferred); 33 } 34 else 35 { 36 promise.handled = true; 37 38 setTimeout(function() { 39 var callback = promise[PROMISE_STATUS] === STATUS_RESOLVED ? 40 deferred.onFulfilled : deferred.onRejected; 41 42 if (callback) 43 { 44 try { 45 resolve(deferred.promise, callback(promise[PROMISE_VALUE])); 46 } catch (err) { 47 reject(deferred.promise, err); 48 } 49 } 50 else 51 { 52 if (promise[PROMISE_STATUS] === STATUS_RESOLVED) 53 { 54 resolve(deferred.promise, promise[PROMISE_VALUE]); 55 } 56 else 57 { 58 reject(deferred.promise, promise[PROMISE_VALUE]); 59 } 60 } 61 }, 0); 62 } 63 }; 64 65 66 /** 67 * Resolves promise with value 68 * @param {Promise} promise 69 * @param {*} value 70 */ 71 var resolve = function(promise, value) { 72 if (value === promise) 73 { 74 throw new TypeError('A promise cannot be resolved with it promise.'); 75 } 76 77 try { 78 if (value && (typeof value === 'object' || typeof value === 'function')) 79 { 80 if (value instanceof Promise) 81 { 82 promise[PROMISE_STATUS] = STATUS_INTERNAL_PENDING; 83 promise[PROMISE_VALUE] = value; 84 finale(promise); 85 return; 86 } 87 else if (typeof value.then === 'function') 88 { 89 executePromise(value.then.bind(value), promise); 90 return; 91 } 92 } 93 94 promise[PROMISE_STATUS] = STATUS_RESOLVED; 95 promise[PROMISE_VALUE] = value; 96 finale(promise); 97 } catch (err) { 98 reject(promise, err); 99 } 100 }; 101 102 103 /** 104 * Rejects promise with reason 105 * @param promise 106 * @param reason 107 */ 108 var reject = function(promise, reason) 109 { 110 promise[PROMISE_STATUS] = STATUS_REJECTED; 111 promise[PROMISE_VALUE] = reason; 112 finale(promise); 113 }; 114 115 116 /** 117 * Calls async callback 118 * @param {Promise} promise 119 */ 120 var finale = function(promise) 121 { 122 if (promise[PROMISE_STATUS] === STATUS_REJECTED && promise.deferreds.length === 0) 123 { 124 setTimeout(function() { 125 if (!promise.handled) 126 { 127 console.error('Unhandled Promise Rejection: ' + promise[PROMISE_VALUE]); 128 } 129 }, 0); 130 } 131 132 promise.deferreds.forEach(function(deferred) { 133 handle(promise, deferred); 134 }); 135 136 promise.deferreds = null; 137 }; 138 139 140 /** 141 * Executes promise 142 * @param {function} resolver - Resolver 143 * @param {Promise} promise 144 */ 145 var executePromise = function(resolver, promise) 146 { 147 var done = false; 148 149 try { 150 resolver(resolveWrapper, rejectWrapper); 151 } catch (err) { 152 if (!done) 153 { 154 done = true; 155 reject(promise, err); 156 } 157 } 158 159 // Resolve function 160 function resolveWrapper(value) 161 { 162 if (!done) 163 { 164 done = true; 165 resolve(promise, value); 166 } 167 } 168 169 // Reject function 170 function rejectWrapper(reason) 171 { 172 if (!done) 173 { 174 done = true; 175 reject(promise, reason); 176 } 177 } 178 }; 179 180 181 /** 182 * Implements interface for works with promise handlers 183 * @param {?function} [onFulfilled] 184 * @param {?function} [onRejected] 185 * @param {Promise} promise 186 * 187 * @constructor 188 */ 189 var Handler = function(onFulfilled, onRejected, promise) 190 { 191 this.onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null; 192 this.onRejected = typeof onRejected === 'function' ? onRejected : null; 193 this.promise = promise; 194 }; 195 196 197 /** 198 * Implements Promise polyfill 199 * @param {function} resolver 200 * 201 * @constructor 202 */ 203 var Promise = function(resolver) 204 { 205 this[PROMISE_STATUS] = STATUS_PENDING; 206 this[PROMISE_VALUE] = null; 207 this.handled = false;
208 this.deferreds = []; 209 210 // Try execute promise resolver 211 executePromise(resolver, this); 212 }; 213 214 215 /** 216 * Appends a rejection handler callback to the promise, 217 * and returns a new promise resolving to the return value of the callback if it is called, 218 * or to its original fulfillment value if the promise is instead fulfilled. 219 * 220 * @param {function} onRejected 221 */ 222 Promise.prototype['catch'] = function(onRejected) { 223 return this.then(null, onRejected); 224 }; 225 226 227 /** 228 * Appends fulfillment and rejection handlers to the promise, 229 * and returns a new promise resolving to the return value of the called handler, 230 * or to its original settled value if the promise was not handled 231 * (i.e. if the relevant handler onFulfilled or onRejected is not a function). 232 * 233 * @param {function} onFulfilled 234 * @param {function} [onRejected] 235 * @returns {Promise} 236 */ 237 Promise.prototype.then = function(onFulfilled, onRejected) 238 { 239 var promise = new Promise(function() {}); 240 handle(this, new Handler(onFulfilled, onRejected, promise)); 241 return promise; 242 }; 243 244 245 /** 246 * The method returns a single Promise that resolves when all of the promises 247 * in the iterable argument have resolved or when the iterable argument contains no promises. 248 * It rejects with the reason of the first promise that rejects. 249 * 250 * @static 251 * @param iterable - An iterable object such as an Array or String 252 * @return {Promise} 253 */ 254 Promise.all = function(iterable) 255 { 256 var args = [].slice.call(iterable); 257 258 return new Promise(function(resolve, reject) { 259 if (args.length === 0) 260 { 261 resolve(args); 262 } 263 else 264 { 265 var remaining = args.length; 266 267 var res = function(i, val) 268 { 269 try { 270 if (val && (typeof val === 'object' || typeof val === 'function')) 271 { 272 if (typeof val.then === 'function') 273 { 274 val.then.call(val, function(val) { 275 res(i, val); 276 }, reject); 277 return; 278 } 279 } 280 281 args[i] = val; 282 283 if (--remaining === 0) 284 { 285 resolve(args); 286 } 287 } catch (ex) { 288 reject(ex); 289 } 290 }; 291 292 for (var i = 0; i < args.length; i++) 293 { 294 res(i, args[i]); 295 } 296 } 297 }); 298 }; 299 300 301 /** 302 * The method returns a Promise object that is resolved with the given value. 303 * If the value is a thenable (i.e. has a 'then' method), 304 * the returned promise will 'follow' that thenable, adopting its eventual state; 305 * if the value was a promise, that object becomes the result of the call to Promise.resolve; 306 * otherwise the returned promise will be fulfilled with the value. 307 * 308 * @param {*|Promise} value - Argument to be resolved by this Promise. Can also be a Promise or a thenable to resolve. 309 * @returns {Promise} 310 */ 311 Promise.resolve = function(value) 312 { 313 if (value && typeof value === 'object' && value.constructor === Promise) 314 { 315 return value; 316 } 317 318 return new Promise(function(resolve) { 319 resolve(value); 320 }); 321 }; 322 323 324 /** 325 * The method returns a Promise object that is rejected with the given reason. 326 * 327 * @param {*} reason - Reason why this Promise rejected. 328 * @returns {Promise} 329 */ 330 Promise.reject = function(reason) 331 { 332 return new Promise(function(resolve, reject) { 333 reject(reason); 334 }); 335 }; 336 337 338 /** 339 * The Promise.race(iterable) method returns a promise that resolves or rejects 340 * as soon as one of the promises in the iterable resolves or rejects, 341 * with the value or reason from that promise. 342 * 343 * @static 344 * @param iterable - An iterable object, such as an Array. 345 * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols 346 * @returns {Promise} 347 */ 348 Promise.race = function(iterable) 349 { 350 return new Promise(function(resolve, reject) { 351 for (var i = 0, len = iterable.length; i < len; i++) 352 { 353 iterable[i].then(resolve, reject); 354 } 355 }); 356 }; 357 358 window.Promise = Promise; 359 } 360})(window); 361/* End */ 362;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.