0

How can I return something from evaluate in a variable?

async function myFunction(page, parentID, elementID)
{
  var childElement; 

  await page.evaluate((parentID, elementID) => {
    var parentElement = getParentElement(parentID);
    childElement = parentElement.getChildElement(parentElement, elementID); // SET THIS VALUE TO var childElement ??

  }, parentID, elementID);

  console.log(childElement.id);
}

Here I want to set something in var childElement. How can I do it?

This thing doesn't work. It says that there is no id of undefined.

1

3 Answers 3

1

Try this:

async function myFunction(page, parentID, elementID)
{
  var childElement = await page.evaluate((parentID, elementID) => {
    var parentElement = getParentElement(parentID);
    return parentElement.getChildElement(parentElement, elementID); 
  }, parentID, elementID);

  console.log(childElement.id);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here you are setting values in "childElement" which was empty at first but later you are extracting childElement.id, so thre are possibility that you arent getting id key in childElement. I suggest first use

 console.log(childElement)

so you will know better, which value you are getting. And you are already setting value of childElement by doing this

childElement = parentElement.getChildElement(parentElement, elementID)

Better check this first, see what exactly you are getting by doing this. Since most of the values are undefined, I cant write a code snippet.

Comments

0

// Can you please try using promises ?
async function myFunction(page, parentID, elementID)
{
    return new Promise((resolve, reject) => {
      page.evaluate((parentID, elementID) => {
        var parentElement = getParentElement(parentID);
        var childElement = parentElement.getChildElement(parentElement, elementID);
    
        resolve(childElement)
      }, parentID, elementID);
      }).catch(function (err) {
        return util.returnExceptionAsPromise(err);
      });
}

// And then call this function like this
myFunction(page, parentID, elementID).then(childElement => {
    console.log('childElement', childElement)
})

4 Comments

This is literally just the same thing as to when using async/await, you just rewrote the answer in promise format which async/await does the same behind the scene.
@famoha I know but I prefer using promises,, that is why suggested this approach.
i mean it just over-complicates a simple matter for the person who asked the question. they used async/await so it would be better to probably also give them an answer in the format they probably understand better and easier.
Yup ! Got your point. Your solution looks more relatable to the question. But still I would go with promises in such situation. No offence :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.