Is it possible to not stop an API call which is doing some operation in server side if i redirect to another page in the mean time in React?

Viewed 45

I used the post method to send some data through an API calling to Nodejs and waiting for the response after getting the response it will trigger another API. At this moment user wants to visit another page but the API calls will not be aborted. API call will do its task. Is it possible to do so?

3 Answers

Call the API in redux using redux-thunk. Since redux is outside of all component. Changing page won't stop the API calling

You can just create a state variable and save the response of the post request in that variable, so it will be saved

When I implement the APIs in express, they continue to be executed even if the user navigates away, as illustrated by this example:

express()
.get("/api1", function(req, res) {
  setTimeout(function() {
    console.log("API call finished");
    res.end();
  }, 10000);
})
.get("/api2", function(req, res) {
  res.end();
})
.listen(...);

The user first types http://server/api1 into their browser, which returns nothing after 10 seconds. But rather than wait, they navigate away to http://server/api2, which returns nothing immediately. But the /api1 call continues, as demonstrated by the console message after 10 seconds.

How does this differ from your case?

Related