Giter VIP home page Giter VIP logo

Comments (4)

andreathniah avatar andreathniah commented on May 11, 2024 2

@thomasdondorf thank you clearing up my misunderstanding. Apologies if it's a basic question, but I'm still not sure how one would go about returning pageTitle. Plain old, calling of pageTitle as a variable outside of the function didn't work.

from puppeteer-cluster.

thomasdondorf avatar thomasdondorf commented on May 11, 2024

This is a misunderstanding regarding the Promise of the cluster.queue function. The Promise of the queue function resolves as soon as the job is queued, not when the job is finished.

This will change in Version 1. queue will not return a Promise anymore and there will be another function called execute which queues the data and waits until the job is finished.

For more discussion see #10

from puppeteer-cluster.

thomasdondorf avatar thomasdondorf commented on May 11, 2024

All good :)

Right now you cannot return data from within the function, but instead you can collect it via an array in the outer scope like this:

const pageTitles = [];
const extractTitle = async ({ page, data: url }) => {
	await page.goto(url);
	const pageTitle = await page.evaluate(() => document.title);
	pageTitles.push(pageTitle);
};

// queue your pages

await cluster.idle(); // wait for the jobs to be finished

// pageTitles should now be filled with your page titles

Alternatively you could also use callbacks to collect your data (or create promises and use their resolve as callback).

Does that clear things up?

from puppeteer-cluster.

andreathniah avatar andreathniah commented on May 11, 2024

@thomasdondorf yes this certainly helps to clear things up! Thank you for helping me out with this!

from puppeteer-cluster.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.