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

nested promises not working #7

Open
vitalets opened this issue Feb 25, 2013 · 3 comments
Open

nested promises not working #7

vitalets opened this issue Feb 25, 2013 · 3 comments

Comments

@vitalets
Copy link

cant get it working with nested promises:
this works:

    Q.ninvoke(db, 'collection', 'users')
    .then(function(collection) {
       throw new Error('123');   // error in first level
      return Q.ninvoke(collection, 'findOne', {url: id})
       .then(function(item) {
        ...

but this don't:

    Q.ninvoke(db, 'collection', 'users')
    .then(function(collection) {
      return Q.ninvoke(collection, 'findOne', {url: id})
       .then(function(item) {
           throw new Error('123');  // error in nested promise
           ...

any help appreciated..

@mbrubin56
Copy link

When you throw an exception inside a promise, you are causing the promise to resolve as a rejection. If you had a fail handler at the end of your chain, you would see the fail handler get called. Promises allow for fail handlers to be attached at any point (even after the promise has been resolved as a rejection or success), so even if you're missing a fail handler in this code, the promise will not leak the exception; it will consider the exception as causing the promise to resolve to a rejection, and will dutifully notify any fail handlers if they are attached in the future. If you want the exception to leak out of the promise wrappers, you need to call end(). If you do that, you will then allow the exception to break free, as it were, of the promise, and get caught by the domain error handler.

@vitalets
Copy link
Author

thanks for explanation.
so the correct code to catch error in nested pormise is:

Q.ninvoke(db, 'collection', 'users')
    .then(function(collection) {
      return Q.ninvoke(collection, 'findOne', {url: id})
       .then(function(item) {
         throw new Error('123');
       });
     })
     .end();

?

@mbrubin56
Copy link

I haven't tested the code, but that should cause your exception to escape the promises and leak out. It should then be able to be caught by your domain's error handler.

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

2 participants