Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Using a CancelToken with an axios instance

I've read several similar questions, but I'm still struggling to be able to cancel my requests given my code structure.

On clicking one of the following elements I'd like to be able to cancel all other requests except the new one, however it seems to cancel all requests, and then prevents axios making any new ones. I'm new to cancelling tokens, so don't really know what I'm doing wrong here:

import {cancelTokenSource} from 'API';

let isActiveRequest;

document.body.addEventListener('click', async(e) => {
    const userElement = e.target.closest('.user-element');
    const appElement = e.target.closest('.app-element');


    if(isActiveRequest) {
        cancelTokenSource.cancel();
        isActiveRequest = false;
    }

    if(userElement) {
        isActiveRequest = true;
        try{
            const users = await Admin.getUsers();
            isActiveRequest = false;
            // display users
        } catch(err) { isActiveRequest = false; }
    }
    if(appElement) {
        isActiveRequest = true;
        try{
            const applications = await Admin.getApplications();
            isActiveRequest = false;
            // display applications
        } catch(err) { isActiveRequest = false; }
    }
});

API.js

export const cancelTokenSource = axios.CancelToken.source();

export default axios.create({
    baseURL: 'http://localhost:8080',
    cancelToken: cancelTokenSource.token
});

Admin.js

export default class Admin {
  
    getUsers({index, limit, orderField, orderDirection}) {
        return API.get('/admin/applicants', { params: { index, limit, orderField, orderDirection } });
    }
    getApplications({index, limit, orderField, orderDirection}) {
        return JRS.get('/admin/applications', { params: { index, limit, orderField, orderDirection} });

    ...
}

Thanks

***Update

Thought maybe I need a new cancel token per request (as maybe it's the old token blocking any new requests), so tried this:

API.js

export let cancelTokenSource;

export default axios.create({
    baseURL: 'http://localhost:8080',
    cancelToken: new axios.CancelToken(c => cancelTokenSource = c),
});

main.js

if(isActiveRequest) {
    cancelTokenSource(); 
    isActiveRequest = false;
}

But no luck. I guess maybe because the instance of axios is the same for each request?

I'm going to try adding the cancel token to the Admin.js functions next

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Yup, adding the cancel token to the individual requests, rather than the axios instance worked:

import {cancelTokenSource} from 'Admin';  // Different

let isActiveRequest;

document.body.addEventListener('click', async(e) => {
    const userElement = e.target.closest('.user-element');
    const appElement = e.target.closest('.app-element');


    if(isActiveRequest) {
        cancelTokenSource();  // Different
        isActiveRequest = false;
    }

    if(userElement) {
        isActiveRequest = true;
        try{
            const users = await Admin.getUsers();
            isActiveRequest = false;
            // display users
        } catch(err) { isActiveRequest = false; }
    }
    if(appElement) {
        isActiveRequest = true;
        try{
            const applications = await Admin.getApplications();
            isActiveRequest = false;
            // display applications
        } catch(err) { isActiveRequest = false; }
    }
});

API.js

// Cancel token removed
export default axios.create({
    baseURL: 'http://localhost:8080',
});

Admin.js

export let cancelTokenSource; // Added from API.js

export default class Admin {
  
    getUsers({index, limit, orderField, orderDirection}) {
        return API.get('/admin/applicants', { 
            params: { index, limit, orderField, orderDirection },
            cancelToken: new axios.CancelToken(c => cancelTokenSource = c)  // Added

        });
    }
    getApplications({index, limit, orderField, orderDirection}) {
        return JRS.get('/admin/applications', { 
            params: { index, limit, orderField, orderDirection} 
            cancelToken: new axios.CancelToken(c => cancelTokenSource = c)  // Added
        });

    ...
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!