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

214
Views
How do I properly resolve a promise list when interacting with a setTimeout() loop?

I'm working on a project that connects to an api in which it first collects some ids in a list, and then it sends another request to that api using that id list.

In there, I have a method where it makes those api calls in a loop that iterates through the ids and collects them within a promise list that is then converted to a normal array using Promise.all(). This worked as expected until I added the setTimeout. Now, the program resolves well before any of the promises have been pushed. I'm pretty inexperienced in Javascript, so I'm sorry if this a pretty newbie question, but how can I modify this code so that it retains the setTimeout on each call while still properly resolving the promise array?

function getSets(list){
return new Promise(function(resolve, reject)
{
    if(typeof list == "string")
    {
        var ids = [list]
    }
    else var ids = list

    //console.log(ids)
    promises = [];
    ids.forEach((id, i) =>
    {
        setTimeout(function()
        {
            promises.push(getRequest('GET', "https://ballchasing.com/api/replays/" + id, success))

        }, i * 5000)
    })
    //resolve(promises)
    Promise.all(promises)
    .then((result) => {
        //console.log('all resolved ', result)
        resolve(result)
    })

})

}

about 4 years ago · Juan Pablo Isaza
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!