1

I have something like:

var a = somePromise.then(function() {
    throw 1;
})
.catch(function() {
    return new PromiseReturningThing();
});

a.catch(function() {alert('err');});   // inside is never called, although I know there is an error

Let's assume that the PromiseReturningThing() returns a promise which will itself throw an exception. With the above code there is no alert showing up. But using this code, it works:

var a = new PromiseReturningThing();
a.catch(function() {alert('err');});

Is there a fundamental thing I did not understand? I couldn't find any example on the net, but I think that it is possible to return promises in catch-blocks.

PromiseReturningThing() is actually PouchDB(), if you're wondering. And the promises in the first snippet are some authentification routines.

I am using Chrome v37.0.2062.120

Thanks in advance!

EDIT

Also weird: if I return null in the first snippet at the place where return new PromiseReturningThing(); is, a.catch doesn't throw an a is undefined exception in Chrome. Why that?

Diptendu
  • 2,120
  • 1
  • 15
  • 28
user2084865
  • 625
  • 1
  • 7
  • 18

1 Answers1

0

Yes, you were right with reducing the example. The actual problem was that my code looked like:

var a = somePromise.then(function() {
    throw 1;
})
.catch(function() {
    return new PromiseReturningThing();
});

a.then(function() {
    // here we can't catch the error anymore:
    a.catch(function() {alert('err');});   // inside is never called, although I know there is an error
});

The last line should be

a.then(function() {
    // ...
}).catch(function() {alert('err');});   // working now

Maybe it'll help someone else to be more careful than I was here. Anyway, thanks for pointing it out to reduce my code :)

user2084865
  • 625
  • 1
  • 7
  • 18