UnhandledPromiseRejectionWarning when running Puppeteer?
为什么会收到以下警告,以及如何消除它们?
警告:
(node:26771) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Protocol error (Runtime.callFunctionOn): Target closed.
(node:26771) [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-zeroexit code.
码:
1 2 3 4 5 6 7 8 9 10 11 | const puppeteer = require("puppeteer"); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto("https://dn.se", { waitUntil:"domcontentloaded" }); var output = page.evaluate(() => { return; }); await browser.close(); })(); |
环境:
- macOS High Sierra
- 节点v8.5.0
- 木偶:1.9.0
您需要
1 2 3 | var output = await page.evaluate(() => { return; }); |
确保您正在使用
1 2 3 4 | process.on('unhandledRejection', (reason, p) => { console.error('Unhandled Rejection at: Promise', p, 'reason:', reason); browser.close(); }); |
您的最终代码应该看起来像这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 'use strict'; const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); process.on('unhandledRejection', (reason, p) => { console.error('Unhandled Rejection at: Promise', p, 'reason:', reason); browser.close(); }); await page.goto('https://dn.se', { waitUntil: 'domcontentloaded', }); var output = await page.evaluate(() => { return; }); await browser.close(); })(); |