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

596
Visualizações
How to handle multiple mutations in parallel with react-query

I have a custom useMutation hook:

  const {
    status: updateScheduleStatus,
    reset: updateScheduleReset,
    mutateAsync: updateSchedule,
  } = useUpdateSchedule(queryClient, jobId as string);

Which I understand sets up the mutation but how would I use this if I wanted to do multiple parallel mutations?

I have tried to implement the following but the mutations execute prior to getting to the Promise.all(mutations line.

        let mutations: Array<any> = [];

        schedulesForDeletion.forEach(async (schedule) => {
          const resp = await monitoringService?.getSchedule(
            schedule.schedule_id,
          );
          mutations.push(
            updateSchedule({
              monitoringService: monitoringService as MonitoringServiceClient,
              schedule,
              etag: resp?.type === "data" ? resp.headers.etag : "",
            }),
          );
        });

        console.dir(mutations);

        await Promise.all(mutations);

I would have through that as mutateAsync returns a Promise that they would not fire in sequence but seems that they do.

Is there a way to handle this in react-query or am I better off just performing this with axios? It would be useful to do in react-query as I need to invalidate some queries when the mutations are successful.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

running multiple mutations in parallel does work with mutateAsync:

const { mutateAsync } = useMutation(num => Promise.resolve(num + 1))

const promise1 = mutateAsync(1)
const promise2 = mutateAsync(2)

await Promise.all([promise1, promise2])

I'm guessing in your example you push a Promise to the array, then you continue your loop and await monitoringService?.getSchedule. Only after that returns, you fire off the second mutation.

So in that sense, it seems that this is what's "blocking" your execution. If you push the original Promise coming from getSchedule, it should work:

schedulesForDeletion.forEach((schedule) => {
  mutations.push(
    monitoringService?.getSchedule(
      schedule.schedule_id,
      ).then(resp => updateSchedule({...})
    )
  )
})
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