MCPcopy
hub / github.com/axios/axios / CancelToken

Class CancelToken

lib/cancel/CancelToken.js:12–133  ·  view source on GitHub ↗

* A `CancelToken` is an object that can be used to request cancellation of an operation. * * @param {Function} executor The executor function. * * @returns {CancelToken}

Source from the content-addressed store, hash-verified

10 * @returns {CancelToken}
11 */
12class CancelToken {
13 constructor(executor) {
14 if (typeof executor !== 'function') {
15 throw new TypeError('executor must be a function.');
16 }
17
18 let resolvePromise;
19
20 this.promise = new Promise(function promiseExecutor(resolve) {
21 resolvePromise = resolve;
22 });
23
24 const token = this;
25
26 // eslint-disable-next-line func-names
27 this.promise.then((cancel) => {
28 if (!token._listeners) return;
29
30 let i = token._listeners.length;
31
32 while (i-- > 0) {
33 token._listeners[i](cancel);
34 }
35 token._listeners = null;
36 });
37
38 // eslint-disable-next-line func-names
39 this.promise.then = (onfulfilled) => {
40 let _resolve;
41 // eslint-disable-next-line func-names
42 const promise = new Promise((resolve) => {
43 token.subscribe(resolve);
44 _resolve = resolve;
45 }).then(onfulfilled);
46
47 promise.cancel = function reject() {
48 token.unsubscribe(_resolve);
49 };
50
51 return promise;
52 };
53
54 executor(function cancel(message, config, request) {
55 if (token.reason) {
56 // Cancellation has already been requested
57 return;
58 }
59
60 token.reason = new CanceledError(message, config, request);
61 resolvePromise(token.reason);
62 });
63 }
64
65 /**
66 * Throws a `CanceledError` if cancellation has been requested.
67 */
68 throwIfRequested() {
69 if (this.reason) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected