vendor: 20,659 bytes, lines 1-680
1// Copyright 2023 Ellucian Company L.P. and its affiliates. 2/*! 3 * @overview es6-promise - a tiny implementation of Promises/A+. 4 * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald) 5 * @license Licensed under MIT license 6 * See https://raw.githubusercontent.com/stefanpenner/es6-promise/master/LICENSE 7 * @version 4.0.5 8 */ 9 10(function (global, factory) { 11 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : 12 typeof define === 'function' && define.amd ? define(factory) : 13 (global.ES6Promise = factory()); 14}(this, (function () { 15 'use strict'; 16 17 function objectOrFunction(x) { 18 return typeof x === 'function' || typeof x === 'object' && x !== null; 19 } 20 21 function isFunction(x) { 22 return typeof x === 'function'; 23 } 24 25 var _isArray = undefined; 26 if (!Array.isArray) { 27 _isArray = function (x) { 28 return Object.prototype.toString.call(x) === '[object Array]'; 29 }; 30 } else { 31 _isArray = Array.isArray; 32 } 33 34 var isArray = _isArray; 35 36 var len = 0; 37 var vertxNext = undefined; 38 var customSchedulerFn = undefined; 39 40 var asap = function asap(callback, arg) { 41 queue[len] = callback; 42 queue[len + 1] = arg; 43 len += 2; 44 if (len === 2) { 45 // If len is 2, that means that we need to schedule an async flush. 46 // If additional callbacks are queued before the queue is flushed, they 47 // will be processed by this flush that we are scheduling. 48 if (customSchedulerFn) { 49 customSchedulerFn(flush); 50 } else { 51 scheduleFlush(); 52 } 53 } 54 }; 55 56 function setScheduler(scheduleFn) { 57 customSchedulerFn = scheduleFn; 58 } 59 60 function setAsap(asapFn) { 61 asap = asapFn; 62 } 63 64 var browserWindow = typeof window !== 'undefined' ? window : undefined; 65 var browserGlobal = browserWindow || {}; 66 var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; 67 var isNode = typeof self === 'undefined' && typeof process !== 'undefined' && ({}).toString.call(process) === '[object process]'; 68 69 // test for web worker but not in IE10 70 var isWorker = typeof Uint8ClampedArray !== 'undefined' && typeof importScripts !== 'undefined' && typeof MessageChannel !== 'undefined'; 71 72 // node 73 function useNextTick() { 74 // node version 0.10.x displays a deprecation warning when nextTick is used recursively 75 // see https://github.com/cujojs/when/issues/410 for details 76 return function () { 77 return process.nextTick(flush); 78 }; 79 } 80 81 // vertx 82 function useVertxTimer() { 83 if (typeof vertxNext !== 'undefined') { 84 return function () { 85 vertxNext(flush); 86 }; 87 } 88 89 return useSetTimeout(); 90 } 91 92 function useMutationObserver() { 93 var iterations = 0; 94 var observer = new BrowserMutationObserver(flush); 95 var node = document.createTextNode(''); 96 observer.observe(node, { characterData: true }); 97 98 return function () { 99 node.data = iterations = ++iterations % 2; 100 }; 101 } 102 103 // web worker 104 function useMessageChannel() { 105 var channel = new MessageChannel(); 106 channel.port1.onmessage = flush; 107 return function () { 108 return channel.port2.postMessage(0); 109 }; 110 } 111 112 function useSetTimeout() { 113 // Store setTimeout reference so es6-promise will be unaffected by 114 // other code modifying setTimeout (like sinon.useFakeTimers()) 115 var globalSetTimeout = setTimeout; 116 return function () { 117 return globalSetTimeout(flush, 1); 118 }; 119 } 120 121 var queue = new Array(1000); 122 function flush() { 123 for (var i = 0; i < len; i += 2) { 124 var callback = queue[i]; 125 var arg = queue[i + 1]; 126 127 callback(arg); 128 129 queue[i] = undefined; 130 queue[i + 1] = undefined; 131 } 132 133 len = 0; 134 } 135 136 function attemptVertx() { 137 try { 138 var r = require; 139 var vertx = r('vertx'); 140 vertxNext = vertx.runOnLoop || vertx.runOnContext; 141 return useVertxTimer(); 142 } catch (e) { 143 return useSetTimeout(); 144 } 145 } 146 147 var scheduleFlush = undefined; 148 // Decide what async method to use to triggering processing of queued callbacks: 149 if (isNode) { 150 scheduleFlush = useNextTick(); 151 } else if (BrowserMutationObserver) { 152 scheduleFlush = useMutationObserver(); 153 } else if (isWorker) { 154 scheduleFlush = useMessageChannel(); 155 } else if (browserWindow === undefined && typeof require === 'function') { 156 scheduleFlush = attemptVertx(); 157 } else { 158 scheduleFlush = useSetTimeout(); 159 } 160 161 function then(onFulfillment, onRejection) { 162 var _arguments = arguments; 163 164 var parent = this; 165 166 var child = new this.constructor(noop); 167 168 if (child[PROMISE_ID] === undefined) { 169 makePromise(child); 170 } 171 172 var _state = parent._state; 173 174 if (_state) { 175 (function () { 176 var callback = _arguments[_state - 1]; 177 asap(function () { 178 return invokeCallback(_state, child, callback, parent._result); 179 }); 180 })(); 181 } else { 182 subscribe(parent, child, onFulfillment, onRejection); 183 } 184 185 return child; 186 } 187 188 /** 189 `Promise.resolve` returns a promise that will become resolved with the 190 passed `value`. It is shorthand for the following: 191 192 ```javascript 193 let promise = new Promise(function(resolve, reject){ 194 resolve(1); 195 }); 196 197 promise.then(function(value){ 198 // value === 1 199 }); 200 ``` 201 202 Instead of writing the above, your code now simply becomes the following: 203 204 ```javascript 205 let promise = Promise.resolve(1); 206 207 promise.then(function(value){ 208 // value === 1 209 }); 210 ``` 211 212 @method resolve 213 @static 214 @param {Any} value value that the returned promise will be resolved with 215 Useful for tooling. 216 @return {Promise} a promise that will become fulfilled with the given 217 `value` 218 */ 219 function resolve(object) { 220 /*jshint validthis:true */ 221 var Constructor = this; 222 223 if (object && typeof object === 'object' && object.constructor === Constructor) { 224 return object; 225 } 226 227 var promise = new Constructor(noop); 228 _resolve(promise, object); 229 return promise; 230 } 231 232 var PROMISE_ID = Math.random().toString(36).substring(16); 233 234 function noop() { } 235 236 var PENDING = void 0; 237 var FULFILLED = 1; 238 var REJECTED = 2; 239 240 var GET_THEN_ERROR = new ErrorObject(); 241 242 function selfFulfillment() { 243 return new TypeError("You cannot resolve a promise with itself"); 244 } 245 246 function cannotReturnOwn() { 247 return new TypeError('A promises callback cannot return that same promise.'); 248 } 249 250 function getThen(promise) { 251 try { 252 return promise.then; 253 } catch (error) { 254 GET_THEN_ERROR.error = error; 255 return GET_THEN_ERROR; 256 } 257 } 258 259 function tryThen(then, value, fulfillmentHandler, rejectionHandler) { 260 try { 261 then.call(value, fulfillmentHandler, rejectionHandler); 262 } catch (e) { 263 return e; 264 } 265 } 266 267 function handleForeignThenable(promise, thenable, then) { 268 asap(function (promise) { 269 var sealed = false; 270 var error = tryThen(then, thenable, function (value) { 271 if (sealed) { 272 return; 273 } 274 sealed = true; 275 if (thenable !== value) { 276 _resolve(promise, value); 277 } else { 278 fulfill(promise, value); 279 } 280 }, function (reason) { 281 if (sealed) { 282 return; 283 } 284 sealed = true; 285 286 _reject(promise, reason); 287 }, 'Settle: ' + (promise._label || ' unknown promise')); 288 289 if (!sealed && error) { 290 sealed = true; 291 _reject(promise, error); 292 } 293 }, promise); 294 } 295 296 function handleOwnThenable(promise, thenable) { 297 if (thenable._state === FULFILLED) { 298 fulfill(promise, thenable._result); 299 } else if (thenable._state === REJECTED) { 300 _reject(promise, thenable._result); 301 } else { 302 subscribe(thenable, undefined, function (value) { 303 return _resolve(promise, value); 304 }, function (reason) { 305 return _reject(promise, reason); 306 }); 307 } 308 } 309 310 function handleMaybeThenable(promise, maybeThenable, then$$) { 311 if (maybeThenable.constructor === promise.constructor && then$$ === then && maybeThenable.constructor.resolve === resolve) { 312 handleOwnThenable(promise, maybeThenable); 313 } else { 314 if (then$$ === GET_THEN_ERROR) { 315 _reject(promise, GET_THEN_ERROR.error); 316 } else if (then$$ === undefined) { 317 fulfill(promise, maybeThenable); 318 } else if (isFunction(then$$)) { 319 handleForeignThenable(promise, maybeThenable, then$$); 320 } else { 321 fulfill(promise, maybeThenable); 322 } 323 } 324 } 325 326 function _resolve(promise, value) { 327 if (promise === value) { 328 _reject(promise, selfFulfillment()); 329 } else if (objectOrFunction(value)) { 330 handleMaybeThenable(promise, value, getThen(value)); 331 } else { 332 fulfill(promise, value); 333 } 334 } 335 336 function publishRejection(promise) { 337 if (promise._onerror) { 338 promise._onerror(promise._result); 339 } 340 341 publish(promise); 342 } 343 344 function fulfill(promise, value) { 345 if (promise._state !== PENDING) { 346 return; 347 } 348 349 promise._result = value; 350 promise._state = FULFILLED; 351 352 if (promise._subscribers.length !== 0) { 353 asap(publish, promise); 354 } 355 } 356 357 function _reject(promise, reason) { 358 if (promise._state !== PENDING) { 359 return; 360 } 361 promise._state = REJECTED; 362 promise._result = reason; 363 364 asap(publishRejection, promise); 365 } 366 367 function subscribe(parent, child, onFulfillment, onRejection) { 368 var _subscribers = parent._subscribers; 369 var length = _subscribers.length; 370 371 parent._onerror = null; 372 373 _subscribers[length] = child; 374 _subscribers[length + FULFILLED] = onFulfillment; 375 _subscribers[length + REJECTED] = onRejection; 376 377 if (length === 0 && parent._state) { 378 asap(publish, parent); 379 } 380 } 381 382 function publish(promise) { 383 var subscribers = promise._subscribers; 384 var settled = promise._state; 385 386 if (subscribers.length === 0) { 387 return; 388 } 389 390 var child = undefined, 391 callback = undefined, 392 detail = promise._result; 393 394 for (var i = 0; i < subscribers.length; i += 3) { 395 child = subscribers[i]; 396 callback = subscribers[i + settled]; 397 398 if (child) { 399 invokeCallback(settled, child, callback, detail); 400 } else { 401 callback(detail); 402 } 403 } 404 405 promise._subscribers.length = 0; 406 } 407 408 function ErrorObject() { 409 this.error = null; 410 } 411 412 var TRY_CATCH_ERROR = new ErrorObject(); 413 414 function tryCatch(callback, detail) { 415 try { 416 return callback(detail); 417 } catch (e) { 418 TRY_CATCH_ERROR.error = e; 419 return TRY_CATCH_ERROR; 420 } 421 } 422 423 function invokeCallback(settled, promise, callback, detail) { 424 var hasCallback = isFunction(callback), 425 value = undefined, 426 error = undefined, 427 succeeded = undefined, 428 failed = undefined; 429 430 if (hasCallback) { 431 value = tryCatch(callback, detail); 432 433 if (value === TRY_CATCH_ERROR) { 434 failed = true; 435 error = value.error; 436 value = null; 437 } else { 438 succeeded = true; 439 } 440 441 if (promise === value) { 442 _reject(promise, cannotReturnOwn()); 443 return; 444 } 445 } else { 446 value = detail; 447 succeeded = true; 448 } 449 450 if (promise._state !== PENDING) { 451 // noop 452 } else if (hasCallback && succeeded) { 453 _resolve(promise, value); 454 } else if (failed) { 455 _reject(promise, error); 456 } else if (settled === FULFILLED) { 457 fulfill(promise, value); 458 } else if (settled === REJECTED) { 459 _reject(promise, value); 460 } 461 } 462 463 function initializePromise(promise, resolver) { 464 try { 465 resolver(function resolvePromise(value) { 466 _resolve(promise, value); 467 }, function rejectPromise(reason) { 468 _reject(promise, reason); 469 }); 470 } catch (e) { 471 _reject(promise, e); 472 } 473 } 474 475 var id = 0; 476 function nextId() { 477 return id++; 478 } 479 480 function makePromise(promise) { 481 promise[PROMISE_ID] = id++; 482 promise._state = undefined; 483 promise._result = undefined; 484 promise._subscribers = []; 485 } 486 487 function Enumerator(Constructor, input) { 488 this._instanceConstructor = Constructor; 489 this.promise = new Constructor(noop); 490 491 if (!this.promise[PROMISE_ID]) { 492 makePromise(this.promise); 493 } 494 495 if (isArray(input)) { 496 this._input = input; 497 this.length = input.length; 498 this._remaining = input.length; 499 500 this._result = new Array(this.length); 501 502 if (this.length === 0) { 503 fulfill(this.promise, this._result); 504 } else { 505 this.length = this.length || 0; 506 this._enumerate(); 507 if (this._remaining === 0) { 508 fulfill(this.promise, this._result); 509 } 510 } 511 } else { 512 _reject(this.promise, validationError()); 513 } 514 } 515 516 function validationError() { 517 return new Error('Array Methods must be provided an Array'); 518 }; 519 520 Enumerator.prototype._enumerate = function () { 521 var length = this.length; 522 var _input = this._input; 523 524 for (var i = 0; this._state === PENDING && i < length; i++) { 525 this._eachEntry(_input[i], i); 526 } 527 }; 528 529 Enumerator.prototype._eachEntry = function (entry, i) { 530 var c = this._instanceConstructor; 531 var resolve$$ = c.resolve; 532 533 if (resolve$$ === resolve) { 534 var _then = getThen(entry); 535 536 if (_then === then && entry._state !== PENDING) { 537 this._settledAt(entry._state, i, entry._result); 538 } else if (typeof _then !== 'function') { 539 this._remaining--; 540 this._result[i] = entry; 541 } else if (c === Promise) { 542 var promise = new c(noop); 543 handleMaybeThenable(promise, entry, _then); 544 this._willSettleAt(promise, i); 545 } else { 546 this._willSettleAt(new c(function (resolve$$) { 547 return resolve$$(entry); 548 }), i); 549 } 550 } else { 551 this._willSettleAt(resolve$$(entry), i); 552 } 553 }; 554 555 Enumerator.prototype._settledAt = function (state, i, value) { 556 var promise = this.promise; 557 558 if (promise._state === PENDING) { 559 this._remaining--; 560 561 if (state === REJECTED) { 562 _reject(promise, value); 563 } else { 564 this._result[i] = value; 565 } 566 } 567 568 if (this._remaining === 0) { 569 fulfill(promise, this._result); 570 } 571 }; 572 573 Enumerator.prototype._willSettleAt = function (promise, i) { 574 var enumerator = this; 575 576 subscribe(promise, undefined, function (value) { 577 return enumerator._settledAt(FULFILLED, i, value); 578 }, function (reason) { 579 return enumerator._settledAt(REJECTED, i, reason); 580 }); 581 }; 582 583 /** 584 `Promise.all` accepts an array of promises, and returns a new promise which 585 is fulfilled with an array of fulfillment values for the passed promises, or 586 rejected with the reason of the first passed promise to be rejected. It casts all 587 elements of the passed iterable to promises as it runs this algorithm. 588 589 Example: 590 591 ```javascript 592 let promise1 = resolve(1); 593 let promise2 = resolve(2); 594 let promise3 = resolve(3); 595 let promises = [ promise1, promise2, promise3 ]; 596 597 Promise.all(promises).then(function(array){ 598 // The array here would be [ 1, 2, 3 ]; 599 }); 600 ``` 601 602 If any of the `promises` given to `all` are rejected, the first promise 603 that is rejected will be given as an argument to the returned promises's 604 rejection handler. For example: 605 606 Example: 607 608 ```javascript 609 let promise1 = resolve(1); 610 let promise2 = reject(new Error("2")); 611 let promise3 = reject(new Error("3")); 612 let promises = [ promise1, promise2, promise3 ]; 613 614 Promise.all(promises).then(function(array){ 615 // Code here never runs because there are rejected promises! 616 }, function(error) { 617 // error.message === "2" 618 }); 619 ``` 620 621 @method all 622 @static 623 @param {Array} entries array of promises 624 @param {String} label optional string for labeling the promise. 625 Useful for tooling. 626 @return {Promise} promise that is fulfilled when all `promises` have been 627 fulfilled, or rejected if any of them become rejected. 628 @static 629 */ 630 function all(entries) { 631 return new Enumerator(this, entries).promise; 632 } 633 634 /** 635 `Promise.race` returns a new promise which is settled in the same way as the 636 first passed promise to settle. 637 638 Example: 639 640 ```javascript 641 let promise1 = new Promise(function(resolve, reject){ 642 setTimeout(function(){ 643 resolve('promise 1'); 644 }, 200); 645 }); 646 647 let promise2 = new Promise(function(resolve, reject){ 648 setTimeout(function(){ 649 resolve('promise 2'); 650 }, 100); 651 }); 652 653 Promise.race([promise1, promise2]).then(function(result){ 654 // result === 'promise 2' because it was resolved before promise1 655 // was resolved. 656 }); 657 ``` 658 659 `Promise.race` is deterministic in that only the state of the first 660 settled promise matters. For example, even if other promises given to the 661 `promises` array argument are resolved, but the first settled promise has 662 become rejected before the other promises became fulfilled, the returned 663 promise will become rejected: 664 665 ```javascript 666 let promise1 = new Promise(function(resolve, reject){ 667 setTimeout(function(){ 668 resolve('promise 1'); 669 }, 200); 670 }); 671 672 let promise2 = new Promise(function(resolve, reject){ 673 setTimeout(function(){ 674 reject(new Error('promise 2')); 675 }, 100); 676 }); 677 678 Promise.race([promise1, promise2]).then(function(result){ 679 // Code here never runs 680 }, function(reason){
681 // reason.message === 'promise 2' because promise 2 became rejected before 682 // promise 1 became fulfilled 683 }); 684 ``` 685 686 An example real-world use case is implementing timeouts: 687 688 ```javascript 689 Promise.race([ajax('foo.json'), timeout(5000)]) 690 ``` 691 692 @method race 693 @static 694 @param {Array} promises array of promises to observe 695 Useful for tooling. 696 @return {Promise} a promise which settles in the same way as the first passed 697 promise to settle. 698 */ 699 function race(entries) { 700 /*jshint validthis:true */ 701 var Constructor = this; 702 703 if (!isArray(entries)) { 704 return new Constructor(function (_, reject) { 705 return reject(new TypeError('You must pass an array to race.')); 706 }); 707 } else { 708 return new Constructor(function (resolve, reject) { 709 var length = entries.length; 710 for (var i = 0; i < length; i++) { 711 Constructor.resolve(entries[i]).then(resolve, reject); 712 } 713 }); 714 } 715 } 716 717 /** 718 `Promise.reject` returns a promise rejected with the passed `reason`. 719 It is shorthand for the following: 720 721 ```javascript 722 let promise = new Promise(function(resolve, reject){ 723 reject(new Error('WHOOPS')); 724 }); 725 726 promise.then(function(value){ 727 // Code here doesn't run because the promise is rejected! 728 }, function(reason){ 729 // reason.message === 'WHOOPS' 730 }); 731 ``` 732 733 Instead of writing the above, your code now simply becomes the following: 734 735 ```javascript 736 let promise = Promise.reject(new Error('WHOOPS')); 737 738 promise.then(function(value){ 739 // Code here doesn't run because the promise is rejected! 740 }, function(reason){ 741 // reason.message === 'WHOOPS' 742 }); 743 ``` 744 745 @method reject 746 @static 747 @param {Any} reason value that the returned promise will be rejected with. 748 Useful for tooling. 749 @return {Promise} a promise rejected with the given `reason`. 750 */ 751 function reject(reason) { 752 /*jshint validthis:true */ 753 var Constructor = this; 754 var promise = new Constructor(noop); 755 _reject(promise, reason); 756 return promise; 757 } 758 759 function needsResolver() { 760 throw new TypeError('You must pass a resolver function as the first argument to the promise constructor'); 761 } 762 763 function needsNew() { 764 throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object c
764onstructor cannot be called as a function."); 765 } 766 767 /** 768 Promise objects represent the eventual result of an asynchronous operation. The 769 primary way of interacting with a promise is through its `then` method, which 770 registers callbacks to receive either a promise's eventual value or the reason 771 why the promise cannot be fulfilled. 772 773 Terminology 774 ----------- 775 776 - `promise` is an object or function with a `then` method whose behavior conforms to this specification. 777 - `thenable` is an object or function that defines a `then` method. 778 - `value` is any legal JavaScript value (including undefined, a thenable, or a promise). 779 - `exception` is a value that is thrown using the throw statement. 780 - `reason` is a value that indicates why a promise was rejected. 781 - `settled` the final resting state of a promise, fulfilled or rejected. 782 783 A promise can be in one of three states: pending, fulfilled, or rejected. 784 785 Promises that are fulfilled have a fulfillment value and are in the fulfilled 786 state. Promises that are rejected have a rejection reason and are in the 787 rejected state. A fulfillment value is never a thenable. 788 789 Promises can also be said to *resolve* a value. If this value is also a 790 promise, then the original promise's settled state will match the value's 791 settled state. So a promise that *resolves* a promise that rejects will 792 itself reject, and a promise that *resolves* a promise that fulfills will 793 itself fulfill. 794 795 796 Basic Usage: 797 ------------ 798 799 ```js 800 let promise = new Promise(function(resolve, reject) { 801 // on success 802 resolve(value); 803 804 // on failure 805 reject(reason); 806 }); 807 808 promise.then(function(value) { 809 // on fulfillment 810 }, function(reason) { 811 // on rejection 812 }); 813 ``` 814 815 Advanced Usage: 816 --------------- 817 818 Promises shine when abstracting away asynchronous interactions such as 819 `XMLHttpRequest`s. 820 821 ```js 822 function getJSON(url) { 823 return new Promise(function(resolve, reject){ 824 let xhr = new XMLHttpRequest(); 825 826 xhr.open('GET', url); 827 xhr.onreadystatechange = handler; 828 xhr.responseType = 'json'; 829 xhr.setRequestHeader('Accept', 'application/json'); 830 xhr.send(); 831 832 function handler() { 833 if (this.readyState === this.DONE) { 834 if (this.status === 200) { 835 resolve(this.response); 836 } else { 837 reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']')); 838 } 839 } 840 }; 841 }); 842 } 843 844 getJSON('/posts.json').then(function(json) { 845 // on fulfillment 846 }, function(reason) { 847 // on rejection 848 }); 849 ``` 850 851 Unlike callbacks, promises are great composable primitives. 852 853 ```js 854 Promise.all([ 855 getJSON('/posts'), 856 getJSON('/comments') 857 ]).then(function(values){ 858 values[0] // => postsJSON 859 values[1] // => commentsJSON 860 861 return values; 862 }); 863 ``` 864 865 @class Promise 866 @param {function} resolver 867 Useful for tooling. 868 @constructor 869 */ 870 function Promise(resolver) { 871 this[PROMISE_ID] = nextId(); 872 this._result = this._state = undefined; 873 this._subscribers = []; 874 875 if (noop !== resolver) { 876 typeof resolver !== 'function' && needsResolver(); 877 this instanceof Promise ? initializePromise(this, resolver) : needsNew(); 878 } 879 } 880 881 Promise.all = all; 882 Promise.race = race; 883 Promise.resolve = resolve; 884 Promise.reject = reject; 885 Promise._setScheduler = setScheduler; 886 Promise._setAsap = setAsap; 887 Promise._asap = asap; 888 889 Promise.prototype = { 890 constructor: Promise, 891 892 /** 893 The primary way of interacting with a promise is through its `then` method, 894 which registers callbacks to receive either a promise's eventual value or the 895 reason why the promise cannot be fulfilled. 896 897 ```js 898 findUser().then(function(user){ 899 // user is available 900 }, function(reason){ 901 // user is unavailable, and you are given the reason why 902 }); 903 ``` 904 905 Chaining 906 -------- 907 908 The return value of `then` is itself a promise. This second, 'downstream' 909 promise is resolved with the return value of the first promise's fulfillment 910 or rejection handler, or rejected if the handler throws an exception. 911 912 ```js 913 findUser().then(function (user) { 914 return user.name; 915 }, function (reason) { 916 return 'default name'; 917 }).then(function (userName) { 918 // If `findUser` fulfilled, `userName` will be the user's name, otherwise it 919 // will be `'default name'` 920 }); 921 922 findUser().then(function (user) { 923 throw new Error('Found user, but still unhappy'); 924 }, function (reason) { 925 throw new Error('`findUser` rejected and we're unhappy'); 926 }).then(function (value) { 927 // never reached 928 }, function (reason) { 929 // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'. 930 // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'. 931 }); 932 ``` 933 If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream. 934 935 ```js 936 findUser().then(function (user) { 937 throw new PedagogicalException('Upstream error'); 938 }).then(function (value) { 939 // never reached 940 }).then(function (value) { 941 // never reached 942 }, function (reason) { 943 // The `PedgagocialException` is propagated all the way down to here 944 }); 945 ``` 946 947 Assimilation 948 ------------ 949 950 Sometimes the value you want to propagate to a downstream promise can only be 951 retrieved asynchronously. This can be achieved by returning a promise in the 952 fulfillment or rejection handler. The downstream promise will then be pending 953 until the returned promise is settled. This is called *assimilation*. 954 955 ```js 956 findUser().then(function (user) { 957 return findCommentsByAuthor(user); 958 }).then(function (comments) { 959 // The user's comments are now available 960 }); 961 ``` 962 963 If the assimliated promise rejects, then the downstream promise will also reject. 964 965 ```js 966 findUser().then(function (user) { 967 return findCommentsByAuthor(user); 968 }).then(function (comments) { 969 // If `findCommentsByAuthor` fulfills, we'll have the value here 970 }, function (reason) { 971 // If `findCommentsByAuthor` rejects, we'll have the reason here 972 }); 973 ``` 974
975 Simple Example 976 -------------- 977 978 Synchronous Example 979 980 ```javascript 981 let result; 982 983 try { 984 result = findResult(); 985 // success 986 } catch(reason) { 987 // failure 988 } 989 ``` 990 991 Errback Example 992 993 ```js 994 findResult(function(result, err){ 995 if (err) { 996 // failure 997 } else { 998 // success 999 } 1000 }); 1001 ``` 1002 1003 Promise Example; 1004 1005 ```javascript 1006 findResult().then(function(result){ 1007 // success 1008 }, function(reason){ 1009 // failure 1010 }); 1011 ``` 1012 1013 Advanced Example 1014 -------------- 1015 1016 Synchronous Example 1017 1018 ```javascript 1019 let author, books; 1020 1021 try { 1022 author = findAuthor(); 1023 books = findBooksByAuthor(author); 1024 // success 1025 } catch(reason) { 1026 // failure 1027 } 1028 ``` 1029 1030 Errback Example 1031 1032 ```js 1033 1034 function foundBooks(books) { 1035 1036 } 1037 1038 function failure(reason) { 1039 1040 } 1041 1042 findAuthor(function(author, err){ 1043 if (err) { 1044 failure(err); 1045 // failure 1046 } else { 1047 try { 1048 findBoooksByAuthor(author, function(books, err) { 1049 if (err) { 1050 failure(err); 1051 } else { 1052 try { 1053 foundBooks(books); 1054 } catch(reason) { 1055 failure(reason); 1056 } 1057 } 1058 }); 1059 } catch(error) { 1060 failure(err); 1061 } 1062 // success 1063 } 1064 }); 1065 ``` 1066 1067 Promise Example; 1068 1069 ```javascript 1070 findAuthor(). 1071 then(findBooksByAuthor). 1072 then(function(books){ 1073 // found books 1074 }).catch(function(reason){ 1075 // something went wrong 1076 }); 1077 ``` 1078 1079 @method then 1080 @param {Function} onFulfilled 1081 @param {Function} onRejected 1082 Useful for tooling. 1083 @return {Promise} 1084 */ 1085 then: then, 1086 1087 /** 1088 `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same 1089 as the catch block of a try/catch statement. 1090 1091 ```js 1092 function findAuthor(){ 1093 throw new Error('couldn't find that author'); 1094 } 1095 1096 // synchronous 1097 try { 1098 findAuthor(); 1099 } catch(reason) { 1100 // something went wrong 1101 } 1102 1103 // async with promises 1104 findAuthor().catch(function(reason){ 1105 // something went wrong 1106 }); 1107 ``` 1108 1109 @method catch 1110 @param {Function} onRejection 1111 Useful for tooling. 1112 @return {Promise} 1113 */ 1114 'catch': function _catch(onRejection) { 1115 return this.then(null, onRejection); 1116 } 1117 }; 1118 1119 function polyfill() { 1120 var local = undefined; 1121 1122 if (typeof global !== 'undefined') { 1123 local = global; 1124 } else if (typeof self !== 'undefined') { 1125 local = self; 1126 } else { 1127 try { 1128 local = Function('return this')(); 1129 } catch (e) { 1130 throw new Error('polyfill failed because global object is unavailable in this environment'); 1131 } 1132 } 1133 1134 var P = local.Promise; 1135 1136 if (P) { 1137 var promiseToString = null; 1138 try { 1139 promiseToString = Object.prototype.toString.call(P.resolve()); 1140 } catch (e) { 1141 // silently ignored 1142 } 1143 1144 if (promiseToString === '[object Promise]' && !P.cast) { 1145 return; 1146 } 1147 } 1148 1149 local.Promise = Promise; 1150 } 1151 1152 // Strange compat.. 1153 Promise.polyfill = polyfill; 1154 Promise.Promise = Promise; 1155 1156 return Promise; 1157 1158}))); 1159 1160ES6Promise.polyfill(); 1161 1162// Copyright 2023 Ellucian Company L.P. and its affiliates. 1163 1164if (!Array.prototype.find) { 1165 Array.prototype.find = function (predicate) { 1166 if (this === null) { 1167 throw new TypeError('Array.prototype.find called on null or undefined'); 1168 } 1169 if (typeof predicate !== 'function') { 1170 throw new TypeError('predicate must be a function'); 1171 } 1172 var list = Object(this); 1173 var length = list.length >>> 0; 1174 var thisArg = arguments[1]; 1175 var value; 1176 1177 for (var i = 0; i < length; i++) { 1178 value = list[i]; 1179 if (predicate.call(thisArg, value, i, list)) { 1180 return value; 1181 } 1182 } 1183 return undefined; 1184 }; 1185} 1186 1187// Copyright 2023 Ellucian Company L.P. and its affiliates. 1188 1189if (!Array.prototype.findIndex) { 1190 Object.defineProperty(Array.prototype, 'findIndex', { 1191 value: function (predicate) { 1192 if (this == null) { 1193 throw new TypeError('"this" is null or not defined'); 1194 } 1195 var o = Object(this); 1196 var len = o.length >>> 0; 1197 if (typeof predicate !== 'function') { 1198 throw new TypeError('predicate must be a function'); 1199 } 1200 var thisArg = arguments[1]; 1201 var k = 0; 1202 while (k < len) { 1203 var kValue = o[k]; 1204 if (predicate.call(thisArg, kValue, k, o)) { 1205 return k; 1206 } 1207 k++; 1208 } 1209 return -1; 1210 }, 1211 configurable: true, 1212 writable: true 1213 });
1214} 1215 1216// Copyright 2023 Ellucian Company L.P. and its affiliates. 1217// Console-polyfill. MIT license. 1218// https://github.com/paulmillr/console-polyfill 1219// Make it safe to do console.log() always. 1220(function (global) { 1221 'use strict'; 1222 if (!global.console) { 1223 global.console = {}; 1224 } 1225 var con = global.console; 1226 var prop, method; 1227 var dummy = function () { }; 1228 var properties = ['memory']; 1229 var methods = ('assert,clear,count,debug,dir,dirxml,error,exception,group,' + 1230 'groupCollapsed,groupEnd,info,log,markTimeline,profile,profiles,profileEnd,' + 1231 'show,table,time,timeEnd,timeline,timelineEnd,timeStamp,trace,warn').split(','); 1232 while (prop = properties.pop()) if (!con[prop]) con[prop] = {}; 1233 while (method = methods.pop()) if (typeof con[method] !== 'function') con[method] = dummy; 1234 // Using `this` for web workers & supports Browserify / Webpack. 1235})(typeof window === 'undefined' ? this : window); 1236 1237// Copyright 2023 Ellucian Company L.P. and its affiliates. 1238if (typeof Object.assign != 'function') { 1239 Object.assign = function (target) { 1240 'use strict'; 1241 if (target == null) { 1242 throw new TypeError('Cannot convert undefined or null to object'); 1243 } 1244 1245 target = Object(target); 1246 for (var index = 1; index < arguments.length; index++) { 1247 var source = arguments[index]; 1248 if (source != null) { 1249 for (var key in source) { 1250 if (Object.prototype.hasOwnProperty.call(source, key)) { 1251 target[key] = source[key]; 1252 } 1253 } 1254 } 1255 } 1256 return target; 1257 }; 1258} 1259 1260/****************************************************/ 1261// 1262// Library that wraps sessionStorage for easier use 1263// 1264// Uses the same interface as sessionStorage, but 1265// handles conversion from JavaScript objects to JSON 1266/****************************************************/ 1267var Ellucian = Ellucian || {}; 1268Ellucian.Storage = Ellucian.Storage || {}; 1269 1270Ellucian.Storage = function (global) { 1271 1272 // Simple function to return the type of an object (more robust/accurate than "typeof") 1273 var toType = function (obj) { 1274 return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase(); 1275 }; 1276 1277 var getItem = function (webStorage, key) { 1278 var item = webStorage.getItem(key); 1279 1280 try { 1281 item = JSON.parse(item); 1282 } catch (e) { 1283 // parse is expected to fail on some types, ignore this and return anyway. 1284 } 1285 1286 return item; 1287 }; 1288 1289 var setItem = function (webStorage, key, value) { 1290 // Get the type of value 1291 var type = toType(value); 1292 1293 // If value is an object or array, stringify it for webStorage, 1294 // as sessionStorage only accepts strings 1295 if (/object|array/.test(type)) { 1296 value = JSON.stringify(value); 1297 } 1298 try { 1299 webStorage.setItem(key, value); 1300 } catch (error) { 1301 // If we don't handle errors, the action could grace in a less than graceful way 1302 // We handle the errors as a no-op, but we could add an Analytics event or log 1303 if (error instanceof DOMException && error.code === 22) { 1304 // Ooops, ran out of webStorage space 1305 } else { 1306 // Some other error 1307 } 1308 } 1309 }; 1310 1311 var removeItem = function (webStorage, key) { 1312 webStorage.removeItem(key); 1313 }; 1314 1315 var clear = function (webStorage) { 1316 webStorage.clear(); 1317 }; 1318 1319 return { 1320 session: { 1321 getItem: function (key) { 1322 return getItem(global.sessionStorage, key); 1323 }, 1324 setItem: function (key, value) { 1325 return setItem(global.sessionStorage, key, value); 1326 }, 1327 removeItem: function (key) { 1328 return removeItem(global.sessionStorage, key); 1329 }, 1330 clear: function () { 1331 return clear(global.sessionStorage); 1332 } 1333 }, 1334 local: { 1335 getItem: function (key) { 1336 return getItem(global.localStorage, key); 1337 }, 1338 setItem: function (key, value) { 1339 return setItem(global.localStorage, key, value); 1340 }, 1341 removeItem: function (key) { 1342 return removeItem(global.localStorage, key); 1343 }, 1344 clear: function () { 1345 return clear(global.localStorage); 1346 } 1347 } 1348 }; 1349}(this); 1350
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.