Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

291
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!