diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-08-27 04:19:34 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-08-27 04:19:34 +0200 |
commit | 665e88c72b568bf25ff0ec8a14109e2504f99aa8 (patch) | |
tree | c380e91e40efa8a47dea9ff833415f17e273d106 /node_modules/axios/lib/cancel/CancelToken.js | |
parent | 24181bdf20e0d23ec5ec5d2eaa08ae1cfb905f0f (diff) |
node_modules
Diffstat (limited to 'node_modules/axios/lib/cancel/CancelToken.js')
-rw-r--r-- | node_modules/axios/lib/cancel/CancelToken.js | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/node_modules/axios/lib/cancel/CancelToken.js b/node_modules/axios/lib/cancel/CancelToken.js new file mode 100644 index 000000000..6b46e6662 --- /dev/null +++ b/node_modules/axios/lib/cancel/CancelToken.js @@ -0,0 +1,57 @@ +'use strict'; + +var Cancel = require('./Cancel'); + +/** + * A `CancelToken` is an object that can be used to request cancellation of an operation. + * + * @class + * @param {Function} executor The executor function. + */ +function CancelToken(executor) { + if (typeof executor !== 'function') { + throw new TypeError('executor must be a function.'); + } + + var resolvePromise; + this.promise = new Promise(function promiseExecutor(resolve) { + resolvePromise = resolve; + }); + + var token = this; + executor(function cancel(message) { + if (token.reason) { + // Cancellation has already been requested + return; + } + + token.reason = new Cancel(message); + resolvePromise(token.reason); + }); +} + +/** + * Throws a `Cancel` if cancellation has been requested. + */ +CancelToken.prototype.throwIfRequested = function throwIfRequested() { + if (this.reason) { + throw this.reason; + } +}; + +/** + * Returns an object that contains a new `CancelToken` and a function that, when called, + * cancels the `CancelToken`. + */ +CancelToken.source = function source() { + var cancel; + var token = new CancelToken(function executor(c) { + cancel = c; + }); + return { + token: token, + cancel: cancel + }; +}; + +module.exports = CancelToken; |