| 1 |
/*! |
| 2 |
* @overview es6-promise - a tiny implementation of Promises/A+. |
| 3 |
* @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald) |
| 4 |
* @license Licensed under MIT license |
| 5 |
* See https://raw.githubusercontent.com/stefanpenner/es6-promise/master/LICENSE |
| 6 |
* @version 4.1.1+f046478d |
| 7 |
*/ |
| 8 |
|
| 9 |
(function (global, factory) { |
| 10 |
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
| 11 |
typeof define === 'function' && define.amd ? define(factory) : |
| 12 |
(global.ES6Promise = factory()); |
| 13 |
}(this, (function () { 'use strict'; |
| 14 |
|
| 15 |
function objectOrFunction(x) { |
| 16 |
var type = typeof x; |
| 17 |
return x !== null && (type === 'object' || type === 'function'); |
| 18 |
} |
| 19 |
|
| 20 |
function isFunction(x) { |
| 21 |
return typeof x === 'function'; |
| 22 |
} |
| 23 |
|
| 24 |
var _isArray = undefined; |
| 25 |
if (Array.isArray) { |
| 26 |
_isArray = Array.isArray; |
| 27 |
} else { |
| 28 |
_isArray = function (x) { |
| 29 |
return Object.prototype.toString.call(x) === '[object Array]'; |
| 30 |
}; |
| 31 |
} |
| 32 |
|
| 33 |
var isArray = _isArray; |
| 34 |
|
| 35 |
var len = 0; |
| 36 |
var vertxNext = undefined; |
| 37 |
var customSchedulerFn = undefined; |
| 38 |
|
| 39 |
var asap = function asap(callback, arg) { |
| 40 |
queue[len] = callback; |
| 41 |
queue[len + 1] = arg; |
| 42 |
len += 2; |
| 43 |
if (len === 2) { |
| 44 |
// If len is 2, that means that we need to schedule an async flush. |
| 45 |
// If additional callbacks are queued before the queue is flushed, they |
| 46 |
// will be processed by this flush that we are scheduling. |
| 47 |
if (customSchedulerFn) { |
| 48 |
customSchedulerFn(flush); |
| 49 |
} else { |
| 50 |
scheduleFlush(); |
| 51 |
} |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
function setScheduler(scheduleFn) { |
| 56 |
customSchedulerFn = scheduleFn; |
| 57 |
} |
| 58 |
|
| 59 |
function setAsap(asapFn) { |
| 60 |
asap = asapFn; |
| 61 |
} |
| 62 |
|
| 63 |
var browserWindow = typeof window !== 'undefined' ? window : undefined; |
| 64 |
var browserGlobal = browserWindow || {}; |
| 65 |
var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver; |
| 66 |
var isNode = typeof self === 'undefined' && typeof process !== 'undefined' && ({}).toString.call(process) === '[object process]'; |
| 67 |
|
| 68 |
// test for web worker but not in IE10 |
| 69 |
var isWorker = typeof Uint8ClampedArray !== 'undefined' && typeof importScripts !== 'undefined' && typeof MessageChannel !== 'undefined'; |
| 70 |
|
| 71 |
// node |
| 72 |
function useNextTick() { |
| 73 |
// node version 0.10.x displays a deprecation warning when nextTick is used recursively |
| 74 |
// see https://github.com/cujojs/when/issues/410 for details |
| 75 |
return function () { |
| 76 |
return process.nextTick(flush); |
| 77 |
}; |
| 78 |
} |
| 79 |
|
| 80 |
// vertx |
| 81 |
function useVertxTimer() { |
| 82 |
if (typeof vertxNext !== 'undefined') { |
| 83 |
return function () { |
| 84 |
vertxNext(flush); |
| 85 |
}; |
| 86 |
} |
| 87 |
|
| 88 |
return useSetTimeout(); |
| 89 |
} |
| 90 |
|
| 91 |
function useMutationObserver() { |
| 92 |
var iterations = 0; |
| 93 |
var observer = new BrowserMutationObserver(flush); |
| 94 |
var node = document.createTextNode(''); |
| 95 |
observer.observe(node, { characterData: true }); |
| 96 |
|
| 97 |
return function () { |
| 98 |
node.data = iterations = ++iterations % 2; |
| 99 |
}; |
| 100 |
} |
| 101 |
|
| 102 |
// web worker |
| 103 |
function useMessageChannel() { |
| 104 |
var channel = new MessageChannel(); |
| 105 |
channel.port1.onmessage = flush; |
| 106 |
return function () { |
| 107 |
return channel.port2.postMessage(0); |
| 108 |
}; |
| 109 |
} |
| 110 |
|
| 111 |
function useSetTimeout() { |
| 112 |
// Store setTimeout reference so es6-promise will be unaffected by |
| 113 |
// other code modifying setTimeout (like sinon.useFakeTimers()) |
| 114 |
var globalSetTimeout = setTimeout; |
| 115 |
return function () { |
| 116 |
return globalSetTimeout(flush, 1); |
| 117 |
}; |
| 118 |
} |
| 119 |
|
| 120 |
var queue = new Array(1000); |
| 121 |
function flush() { |
| 122 |
for (var i = 0; i < len; i += 2) { |
| 123 |
var callback = queue[i]; |
| 124 |
var arg = queue[i + 1]; |
| 125 |
|
| 126 |
callback(arg); |
| 127 |
|
| 128 |
queue[i] = undefined; |
| 129 |
queue[i + 1] = undefined; |
| 130 |
} |
| 131 |
|
| 132 |
len = 0; |
| 133 |
} |
| 134 |
|
| 135 |
function attemptVertx() { |
| 136 |
try { |
| 137 |
var r = require; |
| 138 |
var vertx = r('vertx'); |
| 139 |
vertxNext = vertx.runOnLoop || vertx.runOnContext; |
| 140 |
return useVertxTimer(); |
| 141 |
} catch (e) { |
| 142 |
return useSetTimeout(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
var scheduleFlush = undefined; |
| 147 |
// Decide what async method to use to triggering processing of queued callbacks: |
| 148 |
if (isNode) { |
| 149 |
scheduleFlush = useNextTick(); |
| 150 |
} else if (BrowserMutationObserver) { |
| 151 |
scheduleFlush = useMutationObserver(); |
| 152 |
} else if (isWorker) { |
| 153 |
scheduleFlush = useMessageChannel(); |
| 154 |
} else if (browserWindow === undefined && typeof require === 'function') { |
| 155 |
scheduleFlush = attemptVertx(); |
| 156 |
} else { |
| 157 |
scheduleFlush = useSetTimeout(); |
| 158 |
} |
| 159 |
|
| 160 |
function then(onFulfillment, onRejection) { |
| 161 |
var _arguments = arguments; |
| 162 |
|
| 163 |
var parent = this; |
| 164 |
|
| 165 |
var child = new this.constructor(noop); |
| 166 |
|
| 167 |
if (child[PROMISE_ID] === undefined) { |
| 168 |
makePromise(child); |
| 169 |
} |
| 170 |
|
| 171 |
var _state = parent._state; |
| 172 |
|
| 173 |
if (_state) { |
| 174 |
(function () { |
| 175 |
var callback = _arguments[_state - 1]; |
| 176 |
asap(function () { |
| 177 |
return invokeCallback(_state, child, callback, parent._result); |
| 178 |
}); |
| 179 |
})(); |
| 180 |
} else { |
| 181 |
subscribe(parent, child, onFulfillment, onRejection); |
| 182 |
} |
| 183 |
|
| 184 |
return child; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
`Promise.resolve` returns a promise that will become resolved with the |
| 189 |
passed `value`. It is shorthand for the following: |
| 190 |
|
| 191 |
```javascript |
| 192 |
let promise = new Promise(function(resolve, reject){ |
| 193 |
resolve(1); |
| 194 |
}); |
| 195 |
|
| 196 |
promise.then(function(value){ |
| 197 |
// value === 1 |
| 198 |
}); |
| 199 |
``` |
| 200 |
|
| 201 |
Instead of writing the above, your code now simply becomes the following: |
| 202 |
|
| 203 |
```javascript |
| 204 |
let promise = Promise.resolve(1); |
| 205 |
|
| 206 |
promise.then(function(value){ |
| 207 |
// value === 1 |
| 208 |
}); |
| 209 |
``` |
| 210 |
|
| 211 |
@method resolve |
| 212 |
@static |
| 213 |
@param {Any} value value that the returned promise will be resolved with |
| 214 |
Useful for tooling. |
| 215 |
@return {Promise} a promise that will become fulfilled with the given |
| 216 |
`value` |
| 217 |
*/ |
| 218 |
function resolve$1(object) { |
| 219 |
/*jshint validthis:true */ |
| 220 |
var Constructor = this; |
| 221 |
|
| 222 |
if (object && typeof object === 'object' && object.constructor === Constructor) { |
| 223 |
return object; |
| 224 |
} |
| 225 |
|
| 226 |
var promise = new Constructor(noop); |
| 227 |
resolve(promise, object); |
| 228 |
return promise; |
| 229 |
} |
| 230 |
|
| 231 |
var PROMISE_ID = Math.random().toString(36).substring(16); |
| 232 |
|
| 233 |
function noop() {} |
| 234 |
|
| 235 |
var PENDING = void 0; |
| 236 |
var FULFILLED = 1; |
| 237 |
var REJECTED = 2; |
| 238 |
|
| 239 |
var GET_THEN_ERROR = new ErrorObject(); |
| 240 |
|
| 241 |
function selfFulfillment() { |
| 242 |
return new TypeError("You cannot resolve a promise with itself"); |
| 243 |
} |
| 244 |
|
| 245 |
function cannotReturnOwn() { |
| 246 |
return new TypeError('A promises callback cannot return that same promise.'); |
| 247 |
} |
| 248 |
|
| 249 |
function getThen(promise) { |
| 250 |
try { |
| 251 |
return promise.then; |
| 252 |
} catch (error) { |
| 253 |
GET_THEN_ERROR.error = error; |
| 254 |
return GET_THEN_ERROR; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
function tryThen(then$$1, value, fulfillmentHandler, rejectionHandler) { |
| 259 |
try { |
| 260 |
then$$1.call(value, fulfillmentHandler, rejectionHandler); |
| 261 |
} catch (e) { |
| 262 |
return e; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
function handleForeignThenable(promise, thenable, then$$1) { |
| 267 |
asap(function (promise) { |
| 268 |
var sealed = false; |
| 269 |
var error = tryThen(then$$1, thenable, function (value) { |
| 270 |
if (sealed) { |
| 271 |
return; |
| 272 |
} |
| 273 |
sealed = true; |
| 274 |
if (thenable !== value) { |
| 275 |
resolve(promise, value); |
| 276 |
} else { |
| 277 |
fulfill(promise, value); |
| 278 |
} |
| 279 |
}, function (reason) { |
| 280 |
if (sealed) { |
| 281 |
return; |
| 282 |
} |
| 283 |
sealed = true; |
| 284 |
|
| 285 |
reject(promise, reason); |
| 286 |
}, 'Settle: ' + (promise._label || ' unknown promise')); |
| 287 |
|
| 288 |
if (!sealed && error) { |
| 289 |
sealed = true; |
| 290 |
reject(promise, error); |
| 291 |
} |
| 292 |
}, promise); |
| 293 |
} |
| 294 |
|
| 295 |
function handleOwnThenable(promise, thenable) { |
| 296 |
if (thenable._state === FULFILLED) { |
| 297 |
fulfill(promise, thenable._result); |
| 298 |
} else if (thenable._state === REJECTED) { |
| 299 |
reject(promise, thenable._result); |
| 300 |
} else { |
| 301 |
subscribe(thenable, undefined, function (value) { |
| 302 |
return resolve(promise, value); |
| 303 |
}, function (reason) { |
| 304 |
return reject(promise, reason); |
| 305 |
}); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
function handleMaybeThenable(promise, maybeThenable, then$$1) { |
| 310 |
if (maybeThenable.constructor === promise.constructor && then$$1 === then && maybeThenable.constructor.resolve === resolve$1) { |
| 311 |
handleOwnThenable(promise, maybeThenable); |
| 312 |
} else { |
| 313 |
if (then$$1 === GET_THEN_ERROR) { |
| 314 |
reject(promise, GET_THEN_ERROR.error); |
| 315 |
GET_THEN_ERROR.error = null; |
| 316 |
} else if (then$$1 === undefined) { |
| 317 |
fulfill(promise, maybeThenable); |
| 318 |
} else if (isFunction(then$$1)) { |
| 319 |
handleForeignThenable(promise, maybeThenable, then$$1); |
| 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.error = 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$1(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.length = input.length; |
| 497 |
this._remaining = input.length; |
| 498 |
|
| 499 |
this._result = new Array(this.length); |
| 500 |
|
| 501 |
if (this.length === 0) { |
| 502 |
fulfill(this.promise, this._result); |
| 503 |
} else { |
| 504 |
this.length = this.length || 0; |
| 505 |
this._enumerate(input); |
| 506 |
if (this._remaining === 0) { |
| 507 |
fulfill(this.promise, this._result); |
| 508 |
} |
| 509 |
} |
| 510 |
} else { |
| 511 |
reject(this.promise, validationError()); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
function validationError() { |
| 516 |
return new Error('Array Methods must be provided an Array'); |
| 517 |
} |
| 518 |
|
| 519 |
Enumerator$1.prototype._enumerate = function (input) { |
| 520 |
for (var i = 0; this._state === PENDING && i < input.length; i++) { |
| 521 |
this._eachEntry(input[i], i); |
| 522 |
} |
| 523 |
}; |
| 524 |
|
| 525 |
Enumerator$1.prototype._eachEntry = function (entry, i) { |
| 526 |
var c = this._instanceConstructor; |
| 527 |
var resolve$$1 = c.resolve; |
| 528 |
|
| 529 |
if (resolve$$1 === resolve$1) { |
| 530 |
var _then = getThen(entry); |
| 531 |
|
| 532 |
if (_then === then && entry._state !== PENDING) { |
| 533 |
this._settledAt(entry._state, i, entry._result); |
| 534 |
} else if (typeof _then !== 'function') { |
| 535 |
this._remaining--; |
| 536 |
this._result[i] = entry; |
| 537 |
} else if (c === Promise$3) { |
| 538 |
var promise = new c(noop); |
| 539 |
handleMaybeThenable(promise, entry, _then); |
| 540 |
this._willSettleAt(promise, i); |
| 541 |
} else { |
| 542 |
this._willSettleAt(new c(function (resolve$$1) { |
| 543 |
return resolve$$1(entry); |
| 544 |
}), i); |
| 545 |
} |
| 546 |
} else { |
| 547 |
this._willSettleAt(resolve$$1(entry), i); |
| 548 |
} |
| 549 |
}; |
| 550 |
|
| 551 |
Enumerator$1.prototype._settledAt = function (state, i, value) { |
| 552 |
var promise = this.promise; |
| 553 |
|
| 554 |
if (promise._state === PENDING) { |
| 555 |
this._remaining--; |
| 556 |
|
| 557 |
if (state === REJECTED) { |
| 558 |
reject(promise, value); |
| 559 |
} else { |
| 560 |
this._result[i] = value; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
if (this._remaining === 0) { |
| 565 |
fulfill(promise, this._result); |
| 566 |
} |
| 567 |
}; |
| 568 |
|
| 569 |
Enumerator$1.prototype._willSettleAt = function (promise, i) { |
| 570 |
var enumerator = this; |
| 571 |
|
| 572 |
subscribe(promise, undefined, function (value) { |
| 573 |
return enumerator._settledAt(FULFILLED, i, value); |
| 574 |
}, function (reason) { |
| 575 |
return enumerator._settledAt(REJECTED, i, reason); |
| 576 |
}); |
| 577 |
}; |
| 578 |
|
| 579 |
/** |
| 580 |
`Promise.all` accepts an array of promises, and returns a new promise which |
| 581 |
is fulfilled with an array of fulfillment values for the passed promises, or |
| 582 |
rejected with the reason of the first passed promise to be rejected. It casts all |
| 583 |
elements of the passed iterable to promises as it runs this algorithm. |
| 584 |
|
| 585 |
Example: |
| 586 |
|
| 587 |
```javascript |
| 588 |
let promise1 = resolve(1); |
| 589 |
let promise2 = resolve(2); |
| 590 |
let promise3 = resolve(3); |
| 591 |
let promises = [ promise1, promise2, promise3 ]; |
| 592 |
|
| 593 |
Promise.all(promises).then(function(array){ |
| 594 |
// The array here would be [ 1, 2, 3 ]; |
| 595 |
}); |
| 596 |
``` |
| 597 |
|
| 598 |
If any of the `promises` given to `all` are rejected, the first promise |
| 599 |
that is rejected will be given as an argument to the returned promises's |
| 600 |
rejection handler. For example: |
| 601 |
|
| 602 |
Example: |
| 603 |
|
| 604 |
```javascript |
| 605 |
let promise1 = resolve(1); |
| 606 |
let promise2 = reject(new Error("2")); |
| 607 |
let promise3 = reject(new Error("3")); |
| 608 |
let promises = [ promise1, promise2, promise3 ]; |
| 609 |
|
| 610 |
Promise.all(promises).then(function(array){ |
| 611 |
// Code here never runs because there are rejected promises! |
| 612 |
}, function(error) { |
| 613 |
// error.message === "2" |
| 614 |
}); |
| 615 |
``` |
| 616 |
|
| 617 |
@method all |
| 618 |
@static |
| 619 |
@param {Array} entries array of promises |
| 620 |
@param {String} label optional string for labeling the promise. |
| 621 |
Useful for tooling. |
| 622 |
@return {Promise} promise that is fulfilled when all `promises` have been |
| 623 |
fulfilled, or rejected if any of them become rejected. |
| 624 |
@static |
| 625 |
*/ |
| 626 |
function all$1(entries) { |
| 627 |
return new Enumerator$1(this, entries).promise; |
| 628 |
} |
| 629 |
|
| 630 |
/** |
| 631 |
`Promise.race` returns a new promise which is settled in the same way as the |
| 632 |
first passed promise to settle. |
| 633 |
|
| 634 |
Example: |
| 635 |
|
| 636 |
```javascript |
| 637 |
let promise1 = new Promise(function(resolve, reject){ |
| 638 |
setTimeout(function(){ |
| 639 |
resolve('promise 1'); |
| 640 |
}, 200); |
| 641 |
}); |
| 642 |
|
| 643 |
let promise2 = new Promise(function(resolve, reject){ |
| 644 |
setTimeout(function(){ |
| 645 |
resolve('promise 2'); |
| 646 |
}, 100); |
| 647 |
}); |
| 648 |
|
| 649 |
Promise.race([promise1, promise2]).then(function(result){ |
| 650 |
// result === 'promise 2' because it was resolved before promise1 |
| 651 |
// was resolved. |
| 652 |
}); |
| 653 |
``` |
| 654 |
|
| 655 |
`Promise.race` is deterministic in that only the state of the first |
| 656 |
settled promise matters. For example, even if other promises given to the |
| 657 |
`promises` array argument are resolved, but the first settled promise has |
| 658 |
become rejected before the other promises became fulfilled, the returned |
| 659 |
promise will become rejected: |
| 660 |
|
| 661 |
```javascript |
| 662 |
let promise1 = new Promise(function(resolve, reject){ |
| 663 |
setTimeout(function(){ |
| 664 |
resolve('promise 1'); |
| 665 |
}, 200); |
| 666 |
}); |
| 667 |
|
| 668 |
let promise2 = new Promise(function(resolve, reject){ |
| 669 |
setTimeout(function(){ |
| 670 |
reject(new Error('promise 2')); |
| 671 |
}, 100); |
| 672 |
}); |
| 673 |
|
| 674 |
Promise.race([promise1, promise2]).then(function(result){ |
| 675 |
// Code here never runs |
| 676 |
}, function(reason){ |
| 677 |
// reason.message === 'promise 2' because promise 2 became rejected before |
| 678 |
// promise 1 became fulfilled |
| 679 |
}); |
| 680 |
``` |
| 681 |
|
| 682 |
An example real-world use case is implementing timeouts: |
| 683 |
|
| 684 |
```javascript |
| 685 |
Promise.race([ajax('foo.json'), timeout(5000)]) |
| 686 |
``` |
| 687 |
|
| 688 |
@method race |
| 689 |
@static |
| 690 |
@param {Array} promises array of promises to observe |
| 691 |
Useful for tooling. |
| 692 |
@return {Promise} a promise which settles in the same way as the first passed |
| 693 |
promise to settle. |
| 694 |
*/ |
| 695 |
function race$1(entries) { |
| 696 |
/*jshint validthis:true */ |
| 697 |
var Constructor = this; |
| 698 |
|
| 699 |
if (!isArray(entries)) { |
| 700 |
return new Constructor(function (_, reject) { |
| 701 |
return reject(new TypeError('You must pass an array to race.')); |
| 702 |
}); |
| 703 |
} else { |
| 704 |
return new Constructor(function (resolve, reject) { |
| 705 |
var length = entries.length; |
| 706 |
for (var i = 0; i < length; i++) { |
| 707 |
Constructor.resolve(entries[i]).then(resolve, reject); |
| 708 |
} |
| 709 |
}); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
`Promise.reject` returns a promise rejected with the passed `reason`. |
| 715 |
It is shorthand for the following: |
| 716 |
|
| 717 |
```javascript |
| 718 |
let promise = new Promise(function(resolve, reject){ |
| 719 |
reject(new Error('WHOOPS')); |
| 720 |
}); |
| 721 |
|
| 722 |
promise.then(function(value){ |
| 723 |
// Code here doesn't run because the promise is rejected! |
| 724 |
}, function(reason){ |
| 725 |
// reason.message === 'WHOOPS' |
| 726 |
}); |
| 727 |
``` |
| 728 |
|
| 729 |
Instead of writing the above, your code now simply becomes the following: |
| 730 |
|
| 731 |
```javascript |
| 732 |
let promise = Promise.reject(new Error('WHOOPS')); |
| 733 |
|
| 734 |
promise.then(function(value){ |
| 735 |
// Code here doesn't run because the promise is rejected! |
| 736 |
}, function(reason){ |
| 737 |
// reason.message === 'WHOOPS' |
| 738 |
}); |
| 739 |
``` |
| 740 |
|
| 741 |
@method reject |
| 742 |
@static |
| 743 |
@param {Any} reason value that the returned promise will be rejected with. |
| 744 |
Useful for tooling. |
| 745 |
@return {Promise} a promise rejected with the given `reason`. |
| 746 |
*/ |
| 747 |
function reject$1(reason) { |
| 748 |
/*jshint validthis:true */ |
| 749 |
var Constructor = this; |
| 750 |
var promise = new Constructor(noop); |
| 751 |
reject(promise, reason); |
| 752 |
return promise; |
| 753 |
} |
| 754 |
|
| 755 |
function needsResolver() { |
| 756 |
throw new TypeError('You must pass a resolver function as the first argument to the promise constructor'); |
| 757 |
} |
| 758 |
|
| 759 |
function needsNew() { |
| 760 |
throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function."); |
| 761 |
} |
| 762 |
|
| 763 |
/** |
| 764 |
Promise objects represent the eventual result of an asynchronous operation. The |
| 765 |
primary way of interacting with a promise is through its `then` method, which |
| 766 |
registers callbacks to receive either a promise's eventual value or the reason |
| 767 |
why the promise cannot be fulfilled. |
| 768 |
|
| 769 |
Terminology |
| 770 |
----------- |
| 771 |
|
| 772 |
- `promise` is an object or function with a `then` method whose behavior conforms to this specification. |
| 773 |
- `thenable` is an object or function that defines a `then` method. |
| 774 |
- `value` is any legal JavaScript value (including undefined, a thenable, or a promise). |
| 775 |
- `exception` is a value that is thrown using the throw statement. |
| 776 |
- `reason` is a value that indicates why a promise was rejected. |
| 777 |
- `settled` the final resting state of a promise, fulfilled or rejected. |
| 778 |
|
| 779 |
A promise can be in one of three states: pending, fulfilled, or rejected. |
| 780 |
|
| 781 |
Promises that are fulfilled have a fulfillment value and are in the fulfilled |
| 782 |
state. Promises that are rejected have a rejection reason and are in the |
| 783 |
rejected state. A fulfillment value is never a thenable. |
| 784 |
|
| 785 |
Promises can also be said to *resolve* a value. If this value is also a |
| 786 |
promise, then the original promise's settled state will match the value's |
| 787 |
settled state. So a promise that *resolves* a promise that rejects will |
| 788 |
itself reject, and a promise that *resolves* a promise that fulfills will |
| 789 |
itself fulfill. |
| 790 |
|
| 791 |
|
| 792 |
Basic Usage: |
| 793 |
------------ |
| 794 |
|
| 795 |
```js |
| 796 |
let promise = new Promise(function(resolve, reject) { |
| 797 |
// on success |
| 798 |
resolve(value); |
| 799 |
|
| 800 |
// on failure |
| 801 |
reject(reason); |
| 802 |
}); |
| 803 |
|
| 804 |
promise.then(function(value) { |
| 805 |
// on fulfillment |
| 806 |
}, function(reason) { |
| 807 |
// on rejection |
| 808 |
}); |
| 809 |
``` |
| 810 |
|
| 811 |
Advanced Usage: |
| 812 |
--------------- |
| 813 |
|
| 814 |
Promises shine when abstracting away asynchronous interactions such as |
| 815 |
`XMLHttpRequest`s. |
| 816 |
|
| 817 |
```js |
| 818 |
function getJSON(url) { |
| 819 |
return new Promise(function(resolve, reject){ |
| 820 |
let xhr = new XMLHttpRequest(); |
| 821 |
|
| 822 |
xhr.open('GET', url); |
| 823 |
xhr.onreadystatechange = handler; |
| 824 |
xhr.responseType = 'json'; |
| 825 |
xhr.setRequestHeader('Accept', 'application/json'); |
| 826 |
xhr.send(); |
| 827 |
|
| 828 |
function handler() { |
| 829 |
if (this.readyState === this.DONE) { |
| 830 |
if (this.status === 200) { |
| 831 |
resolve(this.response); |
| 832 |
} else { |
| 833 |
reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']')); |
| 834 |
} |
| 835 |
} |
| 836 |
}; |
| 837 |
}); |
| 838 |
} |
| 839 |
|
| 840 |
getJSON('/posts.json').then(function(json) { |
| 841 |
// on fulfillment |
| 842 |
}, function(reason) { |
| 843 |
// on rejection |
| 844 |
}); |
| 845 |
``` |
| 846 |
|
| 847 |
Unlike callbacks, promises are great composable primitives. |
| 848 |
|
| 849 |
```js |
| 850 |
Promise.all([ |
| 851 |
getJSON('/posts'), |
| 852 |
getJSON('/comments') |
| 853 |
]).then(function(values){ |
| 854 |
values[0] // => postsJSON |
| 855 |
values[1] // => commentsJSON |
| 856 |
|
| 857 |
return values; |
| 858 |
}); |
| 859 |
``` |
| 860 |
|
| 861 |
@class Promise |
| 862 |
@param {function} resolver |
| 863 |
Useful for tooling. |
| 864 |
@constructor |
| 865 |
*/ |
| 866 |
function Promise$3(resolver) { |
| 867 |
this[PROMISE_ID] = nextId(); |
| 868 |
this._result = this._state = undefined; |
| 869 |
this._subscribers = []; |
| 870 |
|
| 871 |
if (noop !== resolver) { |
| 872 |
typeof resolver !== 'function' && needsResolver(); |
| 873 |
this instanceof Promise$3 ? initializePromise(this, resolver) : needsNew(); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
Promise$3.all = all$1; |
| 878 |
Promise$3.race = race$1; |
| 879 |
Promise$3.resolve = resolve$1; |
| 880 |
Promise$3.reject = reject$1; |
| 881 |
Promise$3._setScheduler = setScheduler; |
| 882 |
Promise$3._setAsap = setAsap; |
| 883 |
Promise$3._asap = asap; |
| 884 |
|
| 885 |
Promise$3.prototype = { |
| 886 |
constructor: Promise$3, |
| 887 |
|
| 888 |
/** |
| 889 |
The primary way of interacting with a promise is through its `then` method, |
| 890 |
which registers callbacks to receive either a promise's eventual value or the |
| 891 |
reason why the promise cannot be fulfilled. |
| 892 |
|
| 893 |
```js |
| 894 |
findUser().then(function(user){ |
| 895 |
// user is available |
| 896 |
}, function(reason){ |
| 897 |
// user is unavailable, and you are given the reason why |
| 898 |
}); |
| 899 |
``` |
| 900 |
|
| 901 |
Chaining |
| 902 |
-------- |
| 903 |
|
| 904 |
The return value of `then` is itself a promise. This second, 'downstream' |
| 905 |
promise is resolved with the return value of the first promise's fulfillment |
| 906 |
or rejection handler, or rejected if the handler throws an exception. |
| 907 |
|
| 908 |
```js |
| 909 |
findUser().then(function (user) { |
| 910 |
return user.name; |
| 911 |
}, function (reason) { |
| 912 |
return 'default name'; |
| 913 |
}).then(function (userName) { |
| 914 |
// If `findUser` fulfilled, `userName` will be the user's name, otherwise it |
| 915 |
// will be `'default name'` |
| 916 |
}); |
| 917 |
|
| 918 |
findUser().then(function (user) { |
| 919 |
throw new Error('Found user, but still unhappy'); |
| 920 |
}, function (reason) { |
| 921 |
throw new Error('`findUser` rejected and we're unhappy'); |
| 922 |
}).then(function (value) { |
| 923 |
// never reached |
| 924 |
}, function (reason) { |
| 925 |
// if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'. |
| 926 |
// If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'. |
| 927 |
}); |
| 928 |
``` |
| 929 |
If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream. |
| 930 |
|
| 931 |
```js |
| 932 |
findUser().then(function (user) { |
| 933 |
throw new PedagogicalException('Upstream error'); |
| 934 |
}).then(function (value) { |
| 935 |
// never reached |
| 936 |
}).then(function (value) { |
| 937 |
// never reached |
| 938 |
}, function (reason) { |
| 939 |
// The `PedgagocialException` is propagated all the way down to here |
| 940 |
}); |
| 941 |
``` |
| 942 |
|
| 943 |
Assimilation |
| 944 |
------------ |
| 945 |
|
| 946 |
Sometimes the value you want to propagate to a downstream promise can only be |
| 947 |
retrieved asynchronously. This can be achieved by returning a promise in the |
| 948 |
fulfillment or rejection handler. The downstream promise will then be pending |
| 949 |
until the returned promise is settled. This is called *assimilation*. |
| 950 |
|
| 951 |
```js |
| 952 |
findUser().then(function (user) { |
| 953 |
return findCommentsByAuthor(user); |
| 954 |
}).then(function (comments) { |
| 955 |
// The user's comments are now available |
| 956 |
}); |
| 957 |
``` |
| 958 |
|
| 959 |
If the assimliated promise rejects, then the downstream promise will also reject. |
| 960 |
|
| 961 |
```js |
| 962 |
findUser().then(function (user) { |
| 963 |
return findCommentsByAuthor(user); |
| 964 |
}).then(function (comments) { |
| 965 |
// If `findCommentsByAuthor` fulfills, we'll have the value here |
| 966 |
}, function (reason) { |
| 967 |
// If `findCommentsByAuthor` rejects, we'll have the reason here |
| 968 |
}); |
| 969 |
``` |
| 970 |
|
| 971 |
Simple Example |
| 972 |
-------------- |
| 973 |
|
| 974 |
Synchronous Example |
| 975 |
|
| 976 |
```javascript |
| 977 |
let result; |
| 978 |
|
| 979 |
try { |
| 980 |
result = findResult(); |
| 981 |
// success |
| 982 |
} catch(reason) { |
| 983 |
// failure |
| 984 |
} |
| 985 |
``` |
| 986 |
|
| 987 |
Errback Example |
| 988 |
|
| 989 |
```js |
| 990 |
findResult(function(result, err){ |
| 991 |
if (err) { |
| 992 |
// failure |
| 993 |
} else { |
| 994 |
// success |
| 995 |
} |
| 996 |
}); |
| 997 |
``` |
| 998 |
|
| 999 |
Promise Example; |
| 1000 |
|
| 1001 |
```javascript |
| 1002 |
findResult().then(function(result){ |
| 1003 |
// success |
| 1004 |
}, function(reason){ |
| 1005 |
// failure |
| 1006 |
}); |
| 1007 |
``` |
| 1008 |
|
| 1009 |
Advanced Example |
| 1010 |
-------------- |
| 1011 |
|
| 1012 |
Synchronous Example |
| 1013 |
|
| 1014 |
```javascript |
| 1015 |
let author, books; |
| 1016 |
|
| 1017 |
try { |
| 1018 |
author = findAuthor(); |
| 1019 |
books = findBooksByAuthor(author); |
| 1020 |
// success |
| 1021 |
} catch(reason) { |
| 1022 |
// failure |
| 1023 |
} |
| 1024 |
``` |
| 1025 |
|
| 1026 |
Errback Example |
| 1027 |
|
| 1028 |
```js |
| 1029 |
|
| 1030 |
function foundBooks(books) { |
| 1031 |
|
| 1032 |
} |
| 1033 |
|
| 1034 |
function failure(reason) { |
| 1035 |
|
| 1036 |
} |
| 1037 |
|
| 1038 |
findAuthor(function(author, err){ |
| 1039 |
if (err) { |
| 1040 |
failure(err); |
| 1041 |
// failure |
| 1042 |
} else { |
| 1043 |
try { |
| 1044 |
findBoooksByAuthor(author, function(books, err) { |
| 1045 |
if (err) { |
| 1046 |
failure(err); |
| 1047 |
} else { |
| 1048 |
try { |
| 1049 |
foundBooks(books); |
| 1050 |
} catch(reason) { |
| 1051 |
failure(reason); |
| 1052 |
} |
| 1053 |
} |
| 1054 |
}); |
| 1055 |
} catch(error) { |
| 1056 |
failure(err); |
| 1057 |
} |
| 1058 |
// success |
| 1059 |
} |
| 1060 |
}); |
| 1061 |
``` |
| 1062 |
|
| 1063 |
Promise Example; |
| 1064 |
|
| 1065 |
```javascript |
| 1066 |
findAuthor(). |
| 1067 |
then(findBooksByAuthor). |
| 1068 |
then(function(books){ |
| 1069 |
// found books |
| 1070 |
}).catch(function(reason){ |
| 1071 |
// something went wrong |
| 1072 |
}); |
| 1073 |
``` |
| 1074 |
|
| 1075 |
@method then |
| 1076 |
@param {Function} onFulfilled |
| 1077 |
@param {Function} onRejected |
| 1078 |
Useful for tooling. |
| 1079 |
@return {Promise} |
| 1080 |
*/ |
| 1081 |
then: then, |
| 1082 |
|
| 1083 |
/** |
| 1084 |
`catch` is simply sugar for `then(undefined, onRejection)` which makes it the same |
| 1085 |
as the catch block of a try/catch statement. |
| 1086 |
|
| 1087 |
```js |
| 1088 |
function findAuthor(){ |
| 1089 |
throw new Error('couldn't find that author'); |
| 1090 |
} |
| 1091 |
|
| 1092 |
// synchronous |
| 1093 |
try { |
| 1094 |
findAuthor(); |
| 1095 |
} catch(reason) { |
| 1096 |
// something went wrong |
| 1097 |
} |
| 1098 |
|
| 1099 |
// async with promises |
| 1100 |
findAuthor().catch(function(reason){ |
| 1101 |
// something went wrong |
| 1102 |
}); |
| 1103 |
``` |
| 1104 |
|
| 1105 |
@method catch |
| 1106 |
@param {Function} onRejection |
| 1107 |
Useful for tooling. |
| 1108 |
@return {Promise} |
| 1109 |
*/ |
| 1110 |
'catch': function _catch(onRejection) { |
| 1111 |
return this.then(null, onRejection); |
| 1112 |
} |
| 1113 |
}; |
| 1114 |
|
| 1115 |
/*global self*/ |
| 1116 |
function polyfill$1() { |
| 1117 |
var local = undefined; |
| 1118 |
|
| 1119 |
if (typeof global !== 'undefined') { |
| 1120 |
local = global; |
| 1121 |
} else if (typeof self !== 'undefined') { |
| 1122 |
local = self; |
| 1123 |
} else { |
| 1124 |
try { |
| 1125 |
local = Function('return this')(); |
| 1126 |
} catch (e) { |
| 1127 |
throw new Error('polyfill failed because global object is unavailable in this environment'); |
| 1128 |
} |
| 1129 |
} |
| 1130 |
|
| 1131 |
var P = local.Promise; |
| 1132 |
|
| 1133 |
if (P) { |
| 1134 |
var promiseToString = null; |
| 1135 |
try { |
| 1136 |
promiseToString = Object.prototype.toString.call(P.resolve()); |
| 1137 |
} catch (e) { |
| 1138 |
// silently ignored |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (promiseToString === '[object Promise]' && !P.cast) { |
| 1142 |
return; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
local.Promise = Promise$3; |
| 1147 |
} |
| 1148 |
|
| 1149 |
// Strange compat.. |
| 1150 |
Promise$3.polyfill = polyfill$1; |
| 1151 |
Promise$3.Promise = Promise$3; |
| 1152 |
|
| 1153 |
Promise$3.polyfill(); |
| 1154 |
|
| 1155 |
return Promise$3; |
| 1156 |
|
| 1157 |
}))); |
| 1158 |
|
| 1159 |
|