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

251
Views
Discord Bot JS interaction reply does not wait until the code finish executing

I just started to code and trying to build a discord js bot. I am scraping data from a website. My problem is that the await interaction.reply(randomFact); will execute immediately while my code have not finish scraping and return the result. I have tried async/await but still does not work.

module.exports = {
    data: new SlashCommandBuilder()
        .setName("tips")
        .setDescription("Scrap from website"),
    async execute(interaction) {
        let randomFact;
        let healthArray = [];
            const browser = await puppeteer.launch();

            const page = await browser.newPage();
            await page.goto("https://www.example.com/health-facts/"); 

            const pageData = await page.evaluate(() => {
                return {
                    html: document.documentElement.innerHTML,
                };
            });

            const $ = cheerio.load(pageData.html);

            $(".round-number")
                .find("h3")
                .each(function (i, el) {
                    let row = $(el).text().replace(/(\s+)/g, " ");
                    row = $(el)
                        .text()
                        .replace(/[0-9]+. /g, "")
                        .trim();
                    healthArray.push(row);
                });

            await browser.close();

            randomFact =
                healthArray[Math.floor(Math.random() * healthArray.length)];
        await interaction.reply(randomFact);
    },
};

the output error

Sorry if there is anything lacking in my post. I just joined stack overflow.

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!