-1

Here I'm creating a post request and I want to trigger the status of the request and force it to fail if it took a specific period of time with (pending) status, is that even possible?

    fetch('http://localhost:5000/api', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        message: this.state.input,
      })
    })

I'm looking for some way using axios also, not Fetch API only.

develop05
  • 487
  • 1
  • 9
  • 23

1 Answers1

1

http protocol supports timeout, you can use following config in axios for setting timeout configuration of request

axios({
  method: "post",
  url: 'http://example.com/api',
  timeout: 1000 * 5, // Wait for 5 seconds
  headers: {
    "Content-Type": "application/json"
  },
  data: {
    id: 1234
  }
})
  .then(response => {
    const serverResponse = response.data;
  })
  .catch(error => {
//    catch block with be executed if response take more than 5 seconds
    console.log(error);
});
Saeed Alizadeh
  • 1,417
  • 13
  • 23