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

134
Views
React-Query Async Await firing in wrong order loop

I'm batching mutation requests and firing them off with Promise.all however the callback will sometimes trigger before the mutation has. I can only recreate the issue when deleting single items. I know there's some gotchas when doing async/await within loops but I'm not sure if that's what's causing my problems.

What I'm trying to accomplish:

  1. Gather all the mutations that need to be run (add, delete, and update have different endpoints and must be sent individually).

  2. Trigger all the mutations and wait until they all successfully complete.

  3. Refetch the list with all the updates applied

     useQuery('todos', () => getTodosList(), {
         onSuccess: ({ todos }) => {
             setOriginalList(todos);
         },
     });
    
    const saveUpdates = useCallback(async () => {
    
        const mutations = [];
    
        for (let i = 0; i < list.length; i++) {
            const entry = list[i];
    
            if (entry.modified) {
    
                let mutation;
                const { modified, id } = entry;
    
                if (modified === MODIFICATION.Deleted) {
                    mutation = await deleteMutation.mutateAsync(id);
                } else if (modified === MODIFICATION.Updated) {
                    mutation = await updateMutation.mutateAsync(entry);
                } else if (modified === MODIFICATION.Added) {
                    mutation = await createMutation.mutateAsync(entry);
                }
    
                mutations.push(mutation);
            }
        }
    
        await Promise.all(mutations).then(() => {
            queryClient.invalidateQueries('todos');
        });
    },[...all the dependencies]);
    
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your if/else's you are not assigning a Promise to mutation but the result of the promise. Then you are resolving them all below with await Promise.all

You should'n await in the assignation of the loop

Instead of

if (modified === MODIFICATION.Deleted) {
  mutation = await deleteMutation.mutateAsync(id);
} else if (modified === MODIFICATION.Updated) {
  mutation = await updateMutation.mutateAsync(entry);
} else if (modified === MODIFICATION.Added) {
  mutation = await createMutation.mutateAsync(entry);
}

You should write

if (modified === MODIFICATION.Deleted) {
  mutation = deleteMutation.mutateAsync(id);
} else if (modified === MODIFICATION.Updated) {
  mutation = updateMutation.mutateAsync(entry);
} else if (modified === MODIFICATION.Added) {
  mutation = createMutation.mutateAsync(entry);
}
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!