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

148
Visualizações
Waiting Until All Items in the loop are processed

I click a button and in that for each item in a list, it goes and deletes some stuff. My goal is to disable the button while the deleting is going on so I have a state of isDeleting that handles the enabled/disabled of the button.

Here is how I have set up the change of this isDeleting state. But still the button becomes enabled although some delete processing is still going on.( because I can see the API of deleting is still running in the Network tab of Chrome Dev Tools).

How should I change my code to fully wait until everything is done before changing the state of my isDeleting state?

// Button Click
handleDeleteAllItems = async () => {
    await this.gridApiforEachNodeAfterFilter((node) => {
        this.setState({ isDeleting: true });
        node.setSelected(false);
    });
};

// This code runs for each item in the button click loop
{
    this.setRowSelectable(event.node, false);
    let Id = this.brokerFundsDict[event.data.id].id;
    fetchDeleteFund(someOtherThing.id, id).then(() => {         
        fetchGetFunds(selectedBroker.id).then(() => {
            this.setRowSelectable(event.node, true);
        });
    });
    this.setState({ isDeleting: false });
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use Promise.all like so:

function deleter(someOtherThing) {
  //original: This code runs for each item in the button click loop
  return fetchDeleteFund(someOtherThing.id, id).then(() => {         
    fetchGetFunds(selectedBroker.id).then(() => {
      this.setRowSelectable(event.node, true);
    });
  });
}
function triggerDelete() {
  this.setRowSelectable(event.node, false);

  let allDeletePromises = [];
  toBeDeletedNodes.forEach(node => {
    allDeletePromises.push(deleter(node));
  });

  Promise.all(allDeletePromises)
    .then(arrOfMessages => {
       this.setState({ isDeleting: false });
    })
}
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