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

205
Views
nodejs wait error before return a function value

I want to wait an error before returning value in a function.
My code:

async function CheckIfManagerIsInServer(client, server_id, member) {
    let isIn = { isIn: true, isNotIn: [] };

    if (CheckIfIsInChain(server_id)) {
        database.chains.forEach(chain => {
            if(chain.membersID.includes(server_id)) {
                chain.membersID.forEach(async server => {

                    client.guilds.cache.get(server)?.members.fetch(member).catch((err) => {
                        console.log(err)
                        console.log("not exists")
                        isIn.isIn = false;
                        isIn.isNotIn.push(server)

                        console.log(isIn)
                        return;
                    })

                })
            }
        })

        console.log(isIn)
    }

    console.log(isIn)

    return await Promise.resolve(isIn);
}

As you can see, there is a catch. The catch gets triggered when function has already returned isIn. How I can wait?
Thanks in advance and sorry for bad english!

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

0

use try catch , something like below

try {
await client.guilds.cache.get(server)?.members.fetch(member);
} catch(err) {
                        console.log(err)
                        console.log("not exists")
                        isIn.isIn = false;
                        isIn.isNotIn.push(server)

                        console.log(isIn)
                        return;
                    }

If you want to perform the operation in sequence, you cannot use forEach indeed. Just use a modern for … of loop instead, in which await will work as expected

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!