Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda