chrome-remote-interface: How to know where unhandled promise was triggered (Error: Promise was collected)?
Somewhere in my code I have a promise where I’m not handling a reject. It’s giving me the error:
Logging in
JQMIGRATE: Migrate is installed with logging active, version 1.4.1
(node:1172) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Promise was collected
(node:1172) 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.
Logging in
Go to NEXT page
I looked through my code a few times but didn’t see an issue. The error in question is somewhere in this block:
function login() {
return new Promise((resolve, reject) => {
try {
var userEl = 'input.username';
var passEl = 'input.password';
document.querySelector(userEl).value = 'username';
document.querySelector(passEl).value = 'password';
$('button.submit').click();
setTimeout(resolve, 2000)
} catch (e) {
return reject(e);
}
});
}
And the block of code that triggers this is in a Runtime.evaluate block:
...
let state = 'login';
Page.loadEventFired(async() => {
switch (state) {
case 'login':
console.log('Logging in');
let res = await Runtime.evaluate({
expression: `(${login})()`,
awaitPromise: true,
})
state = 'navigateNextPage';
console.log('Go to NEXT page');
await Page.navigate({
url: 'nextPageUrl',
});
break;
case 'navigateNextPage':
...
break;
...
...
some code to navigate to first login page
...
Seems like in the error, the first time the login invokes in the client, it fails, then the next run is successful. Is there a way to somehow print the stack trace whenever an unhandled Promise rejection occurs?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 24 (11 by maintainers)
Try with
node --trace-warnings? It’ll give you a stack@mahnunchik I added a FAQ entry.
@fracasula see #106, in particular this comment.
@fracasula in you example that promise simply never resolves, try with:
I think I finally understood what’s happening here: the promise gets collected by the garbage collector when the JavaScript execution environment is invalidated, e.g., a page is navigated or reloaded.
You can easily reproduce it like this:
Just make sure there are no pending promises before closing, reloading, etc. a page.
@cyrus-and: yep let me give you a ping if I figure it out.
Update:
--trace-warningsworks great. Will try to get a good stacktrace today.–trace-warnings works great for me but it has to be used BEFORE the file name:
node --trace-warnings file.js
and not
node file.js --trace-warnings # this won’t work