Variable scope in async functions

Tolgay Toklar

I am trying to access a variable that i declared inside in an async function. Like this:

exports.startTheBasketEngine = async (username, password, domainData) => {
    const parse = domainData.auctionUrl.split("-")
    var domainName = parse[0]

    const stringIsIncluded = await page.evaluate(() => {
        console.log(domainName)
     });
}

The console.log line is returning that error:

 UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: domainName is not defined

How can i resolve this problem?

Sebastian Kaczmarek

I assume you use Puppeteer.

According to the docs, the .evaluate() takes more arguments. All arguments, starting from the second, will be passed to the pageFunction. So try something like this:

var domainName = parse[0]

const stringIsIncluded = await page.evaluate((dName) => {
    console.log(dName);
 }, domainName);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Nested async functions variable scope

php variable scope in functions?

JavaScript IIFE functions and variable scope

Variable scope for global inside functions

PHP Laravel Variable Scope in functions

Variable scope for nested functions in python

variable-scope in R functions

How to access the async/await scope variable out of the async function?

Understanding variable scope in nested functions in Python

Variable scope for bash shell scripts and functions in the script

Node JS Nested Functions And Variable Scope

Javascript self executing functions and variable scope

Javascript variable scope when dynamically referencing functions

scope of the variable in c using pointers and functions

Conceptual understanding of variable scope in higher order functions

Understanding variable scope with regard to anonymous functions in EventListeners

How to share a variable between async functions?

Running async functions simultaneously not updating state variable

Is it still valid when the variable passed to `std::async` is out of scope?

How do I assign a variable defined in a synchronous scope in an async function?

Module-scope variable refuses to be overwritten by an async function. Why?

How to best deal with lack of private variable scope inside MySQL functions?

Why doesn't 'with' pass variable scope through nested functions?

How do you handle splicing a variable that is shared among async functions?

Different behavior of async functions when assigning temporary to variable

Variable not in scope: a

Scope of variable

Properly scope functions and methods

Testing functions not on the scope in the controller