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.

JavaScript

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:

JavaScript

This returns ReferenceError because a lives in a different scope.

But this is fine:

JavaScript

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:

JavaScript

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

JavaScript

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

JavaScript

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