diff options
author | Jeffrey Burdges <burdges@gnunet.org> | 2017-08-29 13:41:58 +0200 |
---|---|---|
committer | Jeffrey Burdges <burdges@gnunet.org> | 2017-08-29 13:41:58 +0200 |
commit | 541256ca99875b6007cf6338f7593c8397053514 (patch) | |
tree | 662ca2ee4d1846401bdc826895ad7a8390d54f9a /node_modules/axios/lib/cancel/CancelToken.js | |
parent | 33edef30acda54fc23ec1238d8de13c07a0c87a8 (diff) | |
parent | 52ebba90d6625f78105b94fb4f528bca829cb18f (diff) |
Merge branch 'master' of ssh://taler.net/wallet-webex
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; |