how to set axios timeout

We Are Going To Discuss About how to set axios timeout. So lets Start this Javascript Article.

how to set axios timeout

How to solve how to set axios timeout

With await axios.post('/debug/throw', {timeout: 2000}), actually you send the payload {timeout: 2000} to the server, not set the timeout to 2 seconds. See an example here.
I tested with another syntax of axios and it worked
const test1Press = async () => { console.log("test1 pressed") // obviously not the actual url in this stackoverflow post axios.defaults.baseURL = 'http://localhost:9000' console.log("pre call") console.log(new Date().toUTCString()); try { await axios({ method: 'post', url: '/', timeout: 2000 // only wait for 2s }) console.log(new Date().toUTCString()); console.log("post call passed") } catch (err) { console.log(new Date().toUTCString()); console.log("post call failed") } } test1Press();
On the server-side, I wait for 5 seconds to have the timeout error on the client-side
const http = require('http'); const server = http.createServer(function (req, res) { setTimeout(function () { res.write('Hello World!'); res.end(); }, 5 * 1 * 1000); // after 5s }) .listen(9000);
Running the code above gives me the timeout error after 2 seconds
test1 pressed pre call Mon, 28 Jun 2021 09:01:54 GMT Mon, 28 Jun 2021 09:01:56 GMT post call failed
EDIT
I tested to create the instance of axios, this gives me the same result:
const test1Press = async () => { console.log("test1 pressed") // obviously not the actual url in this stackoverflow post const instance = axios.create({ baseURL: 'http://localhost:9000', timeout: 2000, }); console.log("pre call") console.log(new Date().toUTCString()); try { await instance.post('/'); console.log(new Date().toUTCString()); console.log("post call passed") } catch (err) { console.log(new Date().toUTCString()); console.log("post call failed") } } test1Press();

how to set axios timeout

With await axios.post('/debug/throw', {timeout: 2000}), actually you send the payload {timeout: 2000} to the server, not set the timeout to 2 seconds. See an example here.
I tested with another syntax of axios and it worked
const test1Press = async () => { console.log("test1 pressed") // obviously not the actual url in this stackoverflow post axios.defaults.baseURL = 'http://localhost:9000' console.log("pre call") console.log(new Date().toUTCString()); try { await axios({ method: 'post', url: '/', timeout: 2000 // only wait for 2s }) console.log(new Date().toUTCString()); console.log("post call passed") } catch (err) { console.log(new Date().toUTCString()); console.log("post call failed") } } test1Press();
On the server-side, I wait for 5 seconds to have the timeout error on the client-side
const http = require('http'); const server = http.createServer(function (req, res) { setTimeout(function () { res.write('Hello World!'); res.end(); }, 5 * 1 * 1000); // after 5s }) .listen(9000);
Running the code above gives me the timeout error after 2 seconds
test1 pressed pre call Mon, 28 Jun 2021 09:01:54 GMT Mon, 28 Jun 2021 09:01:56 GMT post call failed
EDIT
I tested to create the instance of axios, this gives me the same result:
const test1Press = async () => { console.log("test1 pressed") // obviously not the actual url in this stackoverflow post const instance = axios.create({ baseURL: 'http://localhost:9000', timeout: 2000, }); console.log("pre call") console.log(new Date().toUTCString()); try { await instance.post('/'); console.log(new Date().toUTCString()); console.log("post call passed") } catch (err) { console.log(new Date().toUTCString()); console.log("post call failed") } } test1Press();

Solution 1

With await axios.post('/debug/throw', {timeout: 2000}), actually you send the payload {timeout: 2000} to the server, not set the timeout to 2 seconds. See an example here.

I tested with another syntax of axios and it worked

const test1Press = async () => {

    console.log("test1 pressed")

    // obviously not the actual url in this stackoverflow post
    axios.defaults.baseURL = 'http://localhost:9000'

    console.log("pre call")
    console.log(new Date().toUTCString());
    try {
        await axios({
            method: 'post',
            url: '/',
            timeout: 2000 // only wait for 2s
        })
        console.log(new Date().toUTCString());
        console.log("post call passed")
    }
    catch (err) {
        console.log(new Date().toUTCString());
        console.log("post call failed")
    }
}

test1Press();

On the server-side, I wait for 5 seconds to have the timeout error on the client-side

const http = require('http');

const server = http.createServer(function (req, res) {
    setTimeout(function () {
        res.write('Hello World!');
        res.end();
    }, 5 * 1 * 1000); // after 5s
})
    .listen(9000);

Running the code above gives me the timeout error after 2 seconds

test1 pressed
pre call
Mon, 28 Jun 2021 09:01:54 GMT
Mon, 28 Jun 2021 09:01:56 GMT
post call failed

EDIT
I tested to create the instance of axios, this gives me the same result:

const test1Press = async () => {

    console.log("test1 pressed")

    // obviously not the actual url in this stackoverflow post
    const instance = axios.create({
        baseURL: 'http://localhost:9000',
        timeout: 2000,
    });

    console.log("pre call")
    console.log(new Date().toUTCString());
    try {
        await instance.post('/');
        console.log(new Date().toUTCString());
        console.log("post call passed")
    }
    catch (err) {
        console.log(new Date().toUTCString());
        console.log("post call failed")
    }
}

test1Press();

Original Author Đăng Khoa Đinh Of This Content

Solution 2

Let’s say you’ve requested the URL through axios and server is taking long time to respond, in this case the axios timeout will work.

But you don’t have internet connection or the IP address or domain name that you’re requesting not there, in this case axios timeout will not work.

See original answer for solution

Original Author Hamza Waleed Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment