0

js]1 say that it is possible to cancel networkrequest (this is about componentWillUnmount() method):

Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, ...

But answers here say it is not possible.

Question being, is it possible or not to cancel a network requests in JS?

For example. I started setInterval with requests using fetch API in componentDidMount. On some condition I want to stop requests. I want to do so in componentWilUnmount. So how can I cancel all network requests? I'm not talking about calling clearInterval which will stop setInterval. I want to make sure that any network requests that were started will be canceled in a given condition.

Green
  • 28,742
  • 61
  • 158
  • 247

1 Answers1

0

Generally this is not always possible. However XHR object has an abort method or axios library has this ability, but basically it will drop the query but the connection remains.

Sergii Vorobei
  • 1,477
  • 13
  • 19