diff options
Diffstat (limited to 'deps/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/node_modules/es6-promisify/node_modules/es6-promise/lib/es6-promise/promise.js')
1 files changed, 72 insertions, 29 deletions
diff --git a/deps/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/node_modules/es6-promisify/node_modules/es6-promise/lib/es6-promise/promise.js b/deps/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/node_modules/es6-promisify/node_modules/es6-promise/lib/es6-promise/promise.js index 20558cbeea..8d8f829c5e 100644 --- a/deps/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/node_modules/es6-promisify/node_modules/es6-promise/lib/es6-promise/promise.js +++ b/deps/npm/node_modules/pacote/node_modules/make-fetch-happen/node_modules/socks-proxy-agent/node_modules/agent-base/node_modules/es6-promisify/node_modules/es6-promise/lib/es6-promise/promise.js @@ -1,14 +1,12 @@ import { isFunction } from './utils'; - import { noop, nextId, PROMISE_ID, initializePromise } from './-internal'; - import { asap, setAsap, @@ -21,7 +19,6 @@ import Resolve from './promise/resolve'; import Reject from './promise/reject'; import then from './then'; - function needsResolver() { throw new TypeError('You must pass a resolver function as the first argument to the promise constructor'); } @@ -129,33 +126,24 @@ function needsNew() { ``` @class Promise - @param {function} resolver + @param {Function} resolver Useful for tooling. @constructor */ -export default function Promise(resolver) { - this[PROMISE_ID] = nextId(); - this._result = this._state = undefined; - this._subscribers = []; - - if (noop !== resolver) { - typeof resolver !== 'function' && needsResolver(); - this instanceof Promise ? initializePromise(this, resolver) : needsNew(); - } -} -Promise.all = all; -Promise.race = race; -Promise.resolve = Resolve; -Promise.reject = Reject; -Promise._setScheduler = setScheduler; -Promise._setAsap = setAsap; -Promise._asap = asap; +class Promise { + constructor(resolver) { + this[PROMISE_ID] = nextId(); + this._result = this._state = undefined; + this._subscribers = []; -Promise.prototype = { - constructor: Promise, + if (noop !== resolver) { + typeof resolver !== 'function' && needsResolver(); + this instanceof Promise ? initializePromise(this, resolver) : needsNew(); + } + } -/** + /** The primary way of interacting with a promise is through its `then` method, which registers callbacks to receive either a promise's eventual value or the reason why the promise cannot be fulfilled. @@ -347,10 +335,9 @@ Promise.prototype = { @param {Function} onRejected Useful for tooling. @return {Promise} -*/ - then: then, + */ -/** + /** `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same as the catch block of a try/catch statement. @@ -376,8 +363,64 @@ Promise.prototype = { @param {Function} onRejection Useful for tooling. @return {Promise} -*/ + */ catch(onRejection) { return this.then(null, onRejection); } -}; + +/** + `finally` will be invoked regardless of the promise's fate just as native + try/catch/finally behaves + + Synchronous example: + + ```js + findAuthor() { + if (Math.random() > 0.5) { + throw new Error(); + } + return new Author(); + } + + try { + return findAuthor(); // succeed or fail + } catch(error) { + return findOtherAuther(); + } finally { + // always runs + // doesn't affect the return value + } + ``` + + Asynchronous example: + + ```js + findAuthor().catch(function(reason){ + return findOtherAuther(); + }).finally(function(){ + // author was either found, or not + }); + ``` + + @method finally + @param {Function} callback + @return {Promise} +*/ + finally(callback) { + let promise = this; + let constructor = promise.constructor; + + return promise.then(value => constructor.resolve(callback()).then(() => value), + reason => constructor.resolve(callback()).then(() => { throw reason; })); + } +} + +Promise.prototype.then = then; +export default Promise; +Promise.all = all; +Promise.race = race; +Promise.resolve = Resolve; +Promise.reject = Reject; +Promise._setScheduler = setScheduler; +Promise._setAsap = setAsap; +Promise._asap = asap; |