Axios Cancel Token

Util to handle axios request cancellation logic

Keeping a Map of request-id -> cancel-source

// cancelTokenUtils.ts
import axios, {CancelToken, CancelTokenSource} from 'axios';

const cancelSources = new Map<string, CancelTokenSource>([]);

function cancelLastRequest(requestId: string): void {
  const token = cancelSources.get(requestId);
  if (token) {

function getCancelToken(requestId: string): CancelToken {
  const nextCancelSource = axios.CancelToken.source();
  cancelSources.set(requestId, nextCancelSource);
  return nextCancelSource.token;

export {getCancelToken, cancelLastRequest};


function getAccessToken(token: string): Promise<{token: string}> {
  return client({
    method: 'post',
    url: '/v1/auth/signin',
    data: {token},
    cancelToken: getCancelToken('session.login'),