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

293
Visualizações
Stop/Break looping from server side with client side?

I have async job inside looping from server side:

const contacts = [];

for(contact of contacts){
    (async () => {
        await MailService.sendMessage(contact, 'message body')
    })()
}

I want to break that looping using client side, what i do is:

1 . from server side, before sendMessage, check if Broadcast status is process or stopped, if stopped, than break looping.

2 . from client side, if i want to stop the Broadcast(stop looping), i need to update Broadcast value from process to stopped.

So, my code should be:

server side:

const contacts = [];

for(contact of contacts){
    (async () => {
        const currentBroadcast = await Broadcast.findById('id')
        if(currentBroadcast.status === 'stopped') break;

        await MailService.sendMessage(contact, 'message body')
    })()
}

client side:

document.getElementById('stop').addEventListener('click', () => {
    fetch('url/broadcast/stop/:id')
})

it works. But wouldn't that make the server heavy? Because in every loop I have to check into the database.

is there any other way i can do to stop the looping? without having to check into the database.

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

0

You don't need a database for that - you just need to update a value that can be seen in the server's code. You could have a plain Express route that adds client session IDs to a collection, which gets updated when the route is called. Then, just have your loop check whether that client's sent such a request.

With sessions, you could have something like, with a route:

const stoppedBroadcastSessionIds = new Set();
app.post('/stopbroadcast', function (req, res) {
  stoppedBroadcastSessionIds.add(req.session.id);
})

and then do

for(const contact of contacts){
  if (stoppedBroadcastSessionIds.has(req.session.id)) break;
  await MailService.sendMessage(contact, 'message body')
}
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