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

288
Views
React - what is the proper way to reload after multiple axios requests are done

I am wondering what is the best way to make multiple axios requests, and reload the page after it's completed.

If using the way below, the page will reload before all the axios requests completed

const handleDeletePost= async (postId) => {
      //delete multiple posts
      let postToBeDeleted = selectedPostId; // it's an array containing the id of selected posts
 
      if (postToBeDeleted.length > 0) {
        postToBeDeleted.forEach(async (id) => {
          const response = await PostService.deletePost(id);
          console.log(response);
        });
        window.location.reload(); // if I put it here, the page will reload before the delete completes
      }

};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use Promise.all to parallelly fetching requests without waiting for each one to be finished:

Also, need to wait for it to be completed first, then reload the page.

const handleDeletePost = async (postId) => {
  const postToBeDeleted = selectedPostId;

  if (postToBeDeleted.length > 0) {
    const promises = [];
    postToBeDeleted.forEach((id) => {
      promises.push(PostService.deletePost(id));
    });
    await Promise.all(promises).then((values) => {
      console.log(values);
    });
    window.location.reload();
  }
};
about 4 years ago · Juan Pablo Isaza Report

0

The problem seems to be because you're expecting forEach to wait on every iteration. However, forEach and async functions do not play well together. You need to use something like Promise.all instead.

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!