QHao
QHao

Reputation: 1

How does puppeteer handle tasks async?

I am new to nodejs and puppeteer, there is a problem to me:

I want to handle multi pages async, is there a way?

My Solution

// main.js

(async () => {
    ...
    await Promise.all([
        twitter.Post(text, uploadImages),
        weibo.Post(text, uploadImages),
        zhihu.Post(text, uploadImages)
    ])
    ...
})();


// twitter/weibo/zhihu.js
async Post() {
    const browser = await puppeteer.launch({ headless: false, userDataDir: './browser' });
    const page = await browser.newPage();
}

Problem

(node:15396) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!


TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

    at onClose (/Users/tinyrat/Desktop/Github/tinyRat/PostOne/node_modules/[email protected]@puppeteer/lib/Launcher.js:348:14)
    at ChildProcess.<anonymous> (/Users/tinyrat/Desktop/Github/tinyRat/PostOne/node_modules/[email protected]@puppeteer/lib/Launcher.js:338:60)
    at ChildProcess.emit (events.js:215:7)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:270:12)
(node:15396) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15396) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
https://www.zhihu.com/api/v3/account/api/login/qrcode/Vj2-1WHTT62tlpv4/image

The problem is that puppeteer can only lanuch 2 processes, it catch a error when lanuching the 3rd process.

Upvotes: 0

Views: 345

Answers (1)

Dadmand
Dadmand

Reputation: 135

const params = {
  args: [
    '--incognito',
    '--ignore-certificate-errors',
    '--no-sandbox',
    '--disable-setuid-sandbox',
    '--window-size=1920,1080',
    '--disable-accelerated-2d-canvas',
    '--disable-gpu'

    // '--unlimited-storage',
    // '--no-startup-window',
    // '--disable-dev-shm-usage',
    // '--disable-crash-reporter',
    // '--disable-breakpad'
  ],
  headless: false
};

(async () => {
    ...
    let promiseArray=[];

    promiseArray.push(twitter.Post(text, uploadImages));
    promiseArray.push(weibo.Post(text, uploadImages));
    promiseArray.push(zhihu.Post(text, uploadImages));

    await Promise.all(promiseArray).then(()=>{
       //success
    }).catch((err)=>console.log(err));

    ...
})();


async function Post() {
  return puppeteer
    .launch(params)
    .then(async (browser) => {
       const page = await browser.newPage();
    });
}

Upvotes: 1

Related Questions