Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Program unable to catch the error thrown by waitForPageToLoad() #91

Open
pofengliu opened this issue Jun 14, 2018 · 0 comments
Open

Program unable to catch the error thrown by waitForPageToLoad() #91

pofengliu opened this issue Jun 14, 2018 · 0 comments

Comments

@pofengliu
Copy link

The way the throw err; inside a Promise constructor which can't be caught by application thus terminate the main thread. Two scenarios listed for comparison, probably should consider reject(err);

`

const promiseTimeout = function (promise, timeout) {
... // https://stackoverflow.com/questions/30936824/what-is-the-best-general-practice-to-timeout-a-function-in-promise
... // Handles the timeout internally, clearing it when the promise resolves.
... // It prevents the library to hang until the timeout is resolved
... var timer = null;
... return Promise.race([new Promise(function (resolve, reject) {
..... timer = setTimeout(function () {
....... var err = new Error('[Headless Chrome] Timeout after ' + timeout + ' ms');
....... err.code = 'TIMEOUT';
....... reject(err); // <== use reject() here!!!!!!!!!
....... }, timeout);
..... return timer;
..... }), promise.then(function (value) {
..... clearTimeout(timer);
..... return value;
..... })]);
... }
undefined

var foo = promiseTimeout(new Promise(function(resolve, reject) {
... setTimeout(function() {
..... return resolve(42)
..... }, 2000)
... }), 1000).catch(err => console.log(err.stack))
undefined
Error: [Headless Chrome] Timeout after 1000 ms
at Timeout._onTimeout (repl:8:17)
at ontimeout (timers.js:458:11)
at tryOnTimeout (timers.js:296:5)
at Timer.listOnTimeout (timers.js:259:5)

////////////////////////////////////////////////////////////////////

const promiseTimeout = function (promise, timeout) {
... // https://stackoverflow.com/questions/30936824/what-is-the-best-general-practice-to-timeout-a-function-in-promise
... // Handles the timeout internally, clearing it when the promise resolves.
... // It prevents the library to hang until the timeout is resolved
... var timer = null;
... return Promise.race([new Promise(function (resolve, reject) {
..... timer = setTimeout(function () {
....... var err = new Error('[Headless Chrome] Timeout after ' + timeout + ' ms');
....... err.code = 'TIMEOUT';
....... throw err;
....... }, timeout);
..... return timer;
..... }), promise.then(function (value) {
..... clearTimeout(timer);
..... return value;
..... })]);
... }
undefined

var foo = promiseTimeout(new Promise(function(resolve, reject) {
... setTimeout(function() {
..... return resolve(42)
..... }, 2000)
... }), 1000).catch(err => console.log(err.stack))
undefined
Error: [Headless Chrome] Timeout after 1000 ms`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant