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

Questions: Shouldn't an error thrown in .catch bubble up? #216

Closed
mohsen1 opened this issue Oct 23, 2015 · 7 comments
Closed

Questions: Shouldn't an error thrown in .catch bubble up? #216

mohsen1 opened this issue Oct 23, 2015 · 7 comments

Comments

@mohsen1
Copy link

mohsen1 commented Oct 23, 2015

I see inconstancy in various implementation for this code snippet:

new Promise(function(resolve, reject) {
  resolve('ok');
})
.then(function(result) {
  throw new Error('boo!');
})
.catch(function(err) {
  throw new Error('hoo boy!');  // Throws in Chrome but nowhere else 
});

Chrome throws that "hoo boy!" error but it doesn't throw anywhere else (Node 4, Safari and Firefox)

@domenic
Copy link
Member

domenic commented Oct 23, 2015

Chrome does not throw that error. It logs it to the developer console, but does not throw it.

Since this isn't a spec bug, it might be better in the future to ask questions like this on Stack Overflow.

@domenic domenic closed this as completed Oct 23, 2015
@mohsen1
Copy link
Author

mohsen1 commented Oct 23, 2015

Is that error uncatchable per spec you mean?

@domenic
Copy link
Member

domenic commented Oct 23, 2015

There's no error at all. It's just like when you use sync XHR and it logs a warning. It's just a developer feature; it doesn't impact the semantics.

@mohsen1
Copy link
Author

mohsen1 commented Oct 23, 2015

So if you throw in .then and .catch of a promise, the only way to to catch it is to use a .catch? I thought the error is going to bubble up and a global handler can catch it...

My assumpsion was based on the fact that most of async methods bubble up their unhanded errors. For example setTimeout:

window.onerror = function(err){ console.log('err ' + err) }

then

setTimeout(function() { throw new Error('hi')}) // prints "err Uncaught Error: hi"

@domenic
Copy link
Member

domenic commented Oct 23, 2015

You may be looking for whatwg/html#224 which is in progress.

@bergus
Copy link

bergus commented Oct 23, 2015

@mohsen1 you can catch it either by chaining another .catch() to your promise or by listening for unhandledRejection events (see https://github.com/domenic/unhandled-rejections-browser-spec, https://github.com/promises-aplus/unhandled-rejections-spec)

@mohsen1
Copy link
Author

mohsen1 commented Oct 23, 2015

Thanks for the links. Where can I read about why this way of handling the error is chosen over simply throwing the error in global context? With async/await we're going to use tons of try/catch blocks anyway...

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

3 participants