Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda