最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

node.js - How to return data from page.evaluate() in Puppeteer, when there is Promise.all() inside browser in JavaScript - Stack

matteradmin6PV0评论

I want to find average color of all images in the page. I am using Puppeteer to launch chromium and then run javascript inside browser.

Problem is that I am using Promies.all to asynchronously calculate all colors inside browser.

I want to return calculated image colors to page.evaluate. How can I do that? Is it possible?

Following code does not work, because imgcolors is no defined obviously, it is returned inside Promise.all callback.

   const returned = await page.evaluate(async () => {
   const fac = new FastAverageColor();
   const imgs = Array.from(document.querySelectorAll('img'));
   const imgpromises = imgs.map(img => fac.getColorAsync(img.src));


   Promise.all(imgpromises).then(imgcolors => { return imgcolors;});

   return {
     document:{
       height: document.body.scrollHeight, 
       width:document.body.scrollWidth 
     },
     imgcolors:imgcolors
   };
 });

I have tried following:

Promise.all(imgpromises).then(imgcolors => { 
      
      return {
        elements:elements, 
        document:{
          height: document.body.scrollHeight, 
          width:document.body.scrollWidth 
        },
        imgcolors:imgcolors
      };
    });

Now I am not returing data to page.evaluate puppeteer function. So const returned is undefined.

How to do that? Thank you in advance!

I want to find average color of all images in the page. I am using Puppeteer to launch chromium and then run javascript inside browser.

Problem is that I am using Promies.all to asynchronously calculate all colors inside browser.

I want to return calculated image colors to page.evaluate. How can I do that? Is it possible?

Following code does not work, because imgcolors is no defined obviously, it is returned inside Promise.all callback.

   const returned = await page.evaluate(async () => {
   const fac = new FastAverageColor();
   const imgs = Array.from(document.querySelectorAll('img'));
   const imgpromises = imgs.map(img => fac.getColorAsync(img.src));


   Promise.all(imgpromises).then(imgcolors => { return imgcolors;});

   return {
     document:{
       height: document.body.scrollHeight, 
       width:document.body.scrollWidth 
     },
     imgcolors:imgcolors
   };
 });

I have tried following:

Promise.all(imgpromises).then(imgcolors => { 
      
      return {
        elements:elements, 
        document:{
          height: document.body.scrollHeight, 
          width:document.body.scrollWidth 
        },
        imgcolors:imgcolors
      };
    });

Now I am not returing data to page.evaluate puppeteer function. So const returned is undefined.

How to do that? Thank you in advance!

Share Improve this question asked Nov 26, 2020 at 0:55 Tomas ZubrikTomas Zubrik 5176 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

In the page.evaluate function, you can return a promise, which will resolve to a value. In this case Promise.all returns a promise, which you can chain to return the value you want. It'd look something like this:

const returned = await page.evaluate(() => {
  const fac = new FastAverageColor();
  const imgs = Array.from(document.querySelectorAll('img'));
  const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
  return Promise.all(imgpromises).then(imgcolors => { 
    return {
      document:{
        height: document.body.scrollHeight, 
        width: document.body.scrollWidth 
      },
      imgcolors: imgcolors
    };
  });
});

Alternatively, since you are using async functions, you can just use the await operator to wait for values to be resolved:

const returned = await page.evaluate(async () => {
  const fac = new FastAverageColor();
  const imgs = Array.from(document.querySelectorAll('img'));
  const imgpromises = imgs.map(img => fac.getColorAsync(img.src));
  const imgcolors = await Promise.all(imgpromises);
  return {
    document:{
      height: document.body.scrollHeight, 
      width: document.body.scrollWidth 
    },
    imgcolors: imgcolors
  };
});

Note that page.evaluate can only return serializable values, i.e. things that can be JSON.stringify'd. It looks like imgcolors should be serializable, but in other cases, you might need to convert the values to something serializable.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far