Taking screenshot in setTimeout causes unhandledPromiseRejectionWarning
See original GitHub issueI’m not sure if this is an issue or just something wrong that I am doing, however if I try to take a screenshot inside a setTimeout I get the error:
(node:11820) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: not opened
(node:11820) [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.
Here is the short script I wrote to highlight the issue I’m having:
const CDP = require('chrome-remote-interface')
const fs = require('fs')
async function example() {
try {
var client = await CDP()
const { Network, Page, Runtime } = client
await Promise.all([Network.enable(), Page.enable()])
await Page.navigate({url: 'https://www.github.com/'})
await Page.loadEventFired()
setTimeout(async () => {
var { data } = await Page.captureScreenshot({format: 'png'})
await fs.writeFile('test.png', data, 'base64', (err) => {
if (err) {
console.error(err)
}
})
}, 5000)
} catch(err) {
console.error(err)
} finally {
if (client) {
await client.close()
}
}
}
example()
If I add the bellow code, the more detailed error message I get is:
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason)
})
Unhandled Rejection at: Promise Promise {
<rejected> Error: not opened
at WebSocket.send (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\ws\lib\WebSocket.js:355:18)
at Chrome.enqueueCommand (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\chrome-remote-interface\lib\chrome.js:124:16)
at C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\chrome-remote-interface\lib\chrome.js:86:28
at Promise (<anonymous>)
at Chrome.send (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\chrome-remote-interface\lib\chrome.js:85:16)
at Object.handler [as captureScreenshot] (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\chrome-remote-interface\lib\api.js:32:23)
at Timeout.setTimeout [as _onTimeout] (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\test.js:15:30)
at ontimeout (timers.js:469:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:264:5) } reason: Error: not opened
at WebSocket.send (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\ws\lib\WebSocket.js:355:18)
at Chrome.enqueueCommand (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\chrome-remote-interface\lib\chrome.js:124:16)
at C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\chrome-remote-interface\lib\chrome.js:86:28
at Promise (<anonymous>)
at Chrome.send (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\chrome-remote-interface\lib\chrome.js:85:16)
at Object.handler [as captureScreenshot] (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\node_modules\chrome-remote-interface\lib\api.js:32:23)
at Timeout.setTimeout [as _onTimeout] (C:\Users\Rowan\Documents\GitHub\StudioBookingAutomator\V3\agent\test.js:15:30)
at ontimeout (timers.js:469:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:264:5)
Any idea what I’m doing wrong?
Component | Version |
---|---|
Operating system | Windows 10 |
Node.js | 8.3.0 |
Chrome/Chromium/… | 60.0.3112.90 |
chrome-remote-interface | 0.24.3 |
Is Chrome running in a container? No
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Cannot stop recursive setTimeout function in my react electron ...
I am working on a screen tracker app which takes screen screenshots every 10 minutes and upload it to a firebase storage. I'm...
Read more >page.screenshot hangs if an alert() on page ready · Issue #2481
With the alert in place, you get a console log for 'loaded page' and then everything hangs indefinitely. A workaround is to use...
Read more >Fixing UnhandledPromiseRejectionWarning in Node.js
JavaScript exhibits asynchronous behaviour for operations that are not completed immediately e.g a HTTP request or timer. These operations ...
Read more >Node.js v19.3.0 Documentation
Using console.log() or similar asynchronous operations inside an AsyncHook callback function will cause an infinite recursion. An easy solution to this when ...
Read more >pact-js
you define different interactions in different tests so there?s no reason why ... I'll see if i can isolate it in a smaller...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As @Jimut says (thanks!) you’re calling
Page.captureScreenshot()
afterclient.close()
, thus on a closed WebSocket. The above comment should fix your problem, but remember that exceptions inside thesetTimeout
are not surfaced to the parenttry
/catch
block.Here’s a possible solution:
@Rauwomos FYI you can use
node --trace-warnings
to obtain the stack trace whenUnhandledPromiseRejectionWarning
errors happens.It’s because
client.close()
is called before thesetTimeout
callback is executed.Try this.