1
votes

I am trying to execute below code,but it throws Unhandled promise rejection warning in the pipelines. Locally it works fine without any issue.

Logs ERROR: The process "3224" not found. (node:836) UnhandledPromiseRejectionWarning: # (node:836) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:836) [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.

Following is the JS code:

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({
      //defaultViewport: null,
      headless:false,
      slowMo: 500,
      timeout: 10000
      //slowMo: 500,
      //devtools: true,
    });

  const page = await browser.newPage();
  await page.goto('https://www.google.com', {waitUntil: 'networkidle2'});
  await page.waitFor(10000);

  await browser.close();
})();
1

1 Answers

1
votes

The pitfall of using async/await is the need to try/catch your asynchronous call in case of an error, instead of using regular .then().catch()

It happens that developers forget to catch the errors, so node.js outputs UnhandledPromiseRejectionWarning: # (node:836) UnhandledPromiseRejectionWarning: Unhandled promise rejection

I am not sure what the error is though, use below instead to debug:

(async () => {
    try {
        const browser = await puppeteer.launch({
            //defaultViewport: null,
            headless: false,
            slowMo: 500,
            timeout: 10000
            //slowMo: 500,
            //devtools: true,
        });

        const page = await browser.newPage();
        await page.goto('https://www.google.com', {
            waitUntil: 'networkidle2'
        });
        await page.waitFor(10000);

        await browser.close();
    } catch (err) {
        console.log(err);
    }
})();

I saw somewhere that node.js might terminate the process in the future if such unhandled promises occur, I am scared to see this to happen ahah