using arrow function into the return block of page.evaluate of puppeter script

alex

It probably is a matter of Promise:

Look at the since field of my return block, using an arrow function it doesn't return any result:

{
    link: 'www.xxxxxx.com/1234',
    name: 'jhon doe',
    since: {}
},

instead to return directly the value, it works as expected!

Since i need to perform complex operations with selectors, I'd like to use an inline arrow function in that point, how can I fix to get the result out?

  let rawMembers = await page.evaluate(() => new Promise((resolve) => { 
    
    ....
    //captute all the link
    const anchors = Array.from(document.querySelectorAll('a'));
    let result = anchors.map(x =>  {  
          return {
            link: x.getAttribute("href"),
            name: x.innerText,
            //since : x.parentNode.parentNode.parentNode.parentNode.getAttribute("class")   <--- this works
            since:  x => {                                                                  <---using an arrow function, it returns and empty objecy `{}`
                // i need a function here to do complex and multiline operations
                return x.parentNode.parentNode.parentNode.parentNode.getAttribute("class");
            }
        

    ....
    resolve(results);

i've tried this as well but with the same result

since:  x => new Promise((resolve) => {
                // i need a function here to do complex and multiline operations
                resolve(x.parentNode.parentNode.parentNode.parentNode.getAttribute("class"));
            })
vsemozhebuty

In since you have a reference to the arrow function itself, not to its result. Functions are not serialaizable, so you get an empty object. You need to call the function, i.e. to use IIFE:

since: (() => {
  return x.parentNode.parentNode.parentNode.parentNode.getAttribute("class");
})()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

using arrow function into the return block of page.evaluate of puppeter script

From Dev

Block arrow using SVG

From Dev

Return page/block number where string was found using perl

From Dev

Return object from arrow function

From Dev

Return object from arrow function

From Dev

Arrow function with return in reduce call

From Dev

phantom-node not passing return value from page.evaluate function

From Dev

return function inside with() block

From Dev

Web Page Redirect and Block Return

From Dev

Evaluate assign function using hash

From Dev

Using Coldfusion's Encrypt function to encrypt a hex block and return a block-length result

From Dev

Using Coldfusion's Encrypt function to encrypt a hex block and return a block-length result

From Java

Refactor: Expected to return a value at the end of arrow function

From Java

Specify return type in TypeScript arrow function

From Dev

Expected to return a value at the end of arrow function with if statement

From Dev

Using `super` within an arrow function within an arrow function within a method

From Dev

Using return keyword in Ruby block

From Dev

Using return in try block (Java)

From Dev

Block script character using regex

From Dev

using evalin to evaluate a function in the base workspace

From Dev

Evaluate a function using a loop Fortran 90

From Dev

`this` is undefined in Dev Tools when using arrow function

From Dev

Using this inside jQuery filter arrow function

From Dev

TypeScript abstract method using lambda/arrow function

From Dev

Cannot stub arrow function in a class using Sinon

From Dev

`this` is undefined in Dev Tools when using arrow function

From Dev

How to update fat arrow function using prototype?

From Dev

Angular 2 only render using arrow function

From Dev

alternative to using arrow function in render with same effect

Related Related

  1. 1

    using arrow function into the return block of page.evaluate of puppeter script

  2. 2

    Block arrow using SVG

  3. 3

    Return page/block number where string was found using perl

  4. 4

    Return object from arrow function

  5. 5

    Return object from arrow function

  6. 6

    Arrow function with return in reduce call

  7. 7

    phantom-node not passing return value from page.evaluate function

  8. 8

    return function inside with() block

  9. 9

    Web Page Redirect and Block Return

  10. 10

    Evaluate assign function using hash

  11. 11

    Using Coldfusion's Encrypt function to encrypt a hex block and return a block-length result

  12. 12

    Using Coldfusion's Encrypt function to encrypt a hex block and return a block-length result

  13. 13

    Refactor: Expected to return a value at the end of arrow function

  14. 14

    Specify return type in TypeScript arrow function

  15. 15

    Expected to return a value at the end of arrow function with if statement

  16. 16

    Using `super` within an arrow function within an arrow function within a method

  17. 17

    Using return keyword in Ruby block

  18. 18

    Using return in try block (Java)

  19. 19

    Block script character using regex

  20. 20

    using evalin to evaluate a function in the base workspace

  21. 21

    Evaluate a function using a loop Fortran 90

  22. 22

    `this` is undefined in Dev Tools when using arrow function

  23. 23

    Using this inside jQuery filter arrow function

  24. 24

    TypeScript abstract method using lambda/arrow function

  25. 25

    Cannot stub arrow function in a class using Sinon

  26. 26

    `this` is undefined in Dev Tools when using arrow function

  27. 27

    How to update fat arrow function using prototype?

  28. 28

    Angular 2 only render using arrow function

  29. 29

    alternative to using arrow function in render with same effect

HotTag

Archive