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

166
Views
Problem with async/await while using .forEach

I'm making a simple Redis' request which is supposed to return all VALUES (not keys) in my database. The problem is that my function returns tab before the .forEach even starts. Why do I know that? My console.log(tab)'s result is printed before console.log(cards) for each iteration. My interpreter also tells me inside the .forEach function that "Promise returned from forEach argument is ignored". What have I done wrong there? Why isn't async/await working inside .forEach?

router.get("/", async (req, res) => {
    try {
        const keys = await client.keys("*")
        //console.log(result)
        const tab = []
        await keys.forEach(async (key) => {
            const cards = await client.smembers(key)
            console.log(cards)
            tab.push(cards)
        })
        console.log(tab)
        return res.send(tab)
    } catch (err) {
        console.error(err)
    }

});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

forEach is not meant to work with async functions. You can use a for ... of loop instead.

for(const key of keys){
    const cards = await client.smembers(key)
    console.log(cards)
    tab.push(cards)
}
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!