Skip to content
Advertisement

How do I return the value I scraped with pupeteer outside of my async function

I am coding a telegram bot using telegraph and I have been running into issues the whole day. What I was trying to do was to make my telegram bot receive the divided held amount and value to print the value of each token, but I cannot figure out how to return the value to bot. Also it throws an exception when I try to run it like this if I leave the bot outside of function. I switched out the links for privacy reasons but the numbers do not matter since they divide correctly.

const { Telegraf } = require('telegraf')
const puppeteer = require("puppeteer-extra")
const stealth = require("puppeteer-extra-plugin-stealth")()
const anon = require(`puppeteer-extra-plugin-anonymize-ua`)()
puppeteer.use(stealth).use(anon);

(async () => {

  const bot = new Telegraf('my telegraf bot ID, can't post it')

    //the token URL
  let tokenUrl = 'https://bscscan.com/tokenholdings?a=0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c';

  let browser = await puppeteer.launch();
  let page = await browser.newPage();

  await page.goto(tokenUrl, { waitUntil: 'networkidle2' });

  let tokenPrice = await page.evaluate(() => {

    let amount = document.querySelector('div[class="table-responsive mb-2 mb-md-0"]>table>tbody> tr:nth-child(4) > td:nth-child(4)').innerText;
    //console.log(amount);
    amount = Number(amount.replace(`,`, ``));

    let holdingPrice = document.querySelector('span[class="h5 mr-1 mb-0"]').innerText;
    //console.log(holdingPrice);
    holdingPrice = Number(holdingPrice.replace(`$`, ``).replace(`,`, ``).replace(`,`, ``).replace(`,`, ``));

    let tokenCurrentPrice = holdingPrice / amount;

    return tokenCurrentPrice;

  });

  console.log(tokenPrice);

})();

//bot.command('price', (ctx) => ctx.reply(tokenPrice))      

Advertisement

Answer

It throws an exception when I try to run it like this if I leave the bot outside of function.

const bot is declared in a different scope. Constants are block-scoped, so the name bot is not defined outside of the scope.

To illustrate the problem:

{
    const a = 5
}

console.log(a);

This returns ReferenceError because a lives in a different scope.

But this is fine:

{
    const a = 5
    console.log(a);
}

I cannot figure out how to return the value to bot.

Your IIHF is an async function, all async functions return a promise. To illustrate this, this won’t print 5 because the promise is not resolved yet:

async function getValue () {
    return 5;
}

console.log(getValue());

If you want to get the value, you need to wait for the promise to get resolved:

async function getValue () {
    return 5;
}

(async () => {
    console.log(await getValue());
})();

Also make sure you don’t use await outside of an async scope:

async function getValue () {
    return 5;
}

console.log(await getValue());

This won’t work and it will give an error. That’s why I used the IIHF with an async scope.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement