我有类似的东西:
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
假设 PromiseReturningThing()
返回一个 Promise,它本身会抛出异常。
使用上面的代码不会出现任何警报。但使用这段代码,它可以工作:
var a = new PromiseReturningThing();
a.catch(function() {alert('err');});
有什么基本的事情我不明白吗?我在网上找不到任何示例,但我认为可以在 catch block 中返回 promise 。
PromiseReturningThing()
如果您想知道的话,实际上是 PouchDB()。第一个片段中的 promise 是一些身份验证例程。
我使用的是 Chrome v37.0.2062.120
提前致谢!
编辑
也很奇怪:如果我 return null
在第一个片段中 return new PromiseReturningThing();
的位置是, a.catch 不会抛出 a is undefined
Chrome 中的异常。为什么会这样?
最佳答案
是的,你减少这个例子是对的。实际的问题是我的代码如下所示:
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
});
最后一行应该是
a.then(function() {
// ...
}).catch(function() {alert('err');}); // working now
也许这会帮助其他人比我在这里更加小心。不管怎样,感谢您指出它以减少我的代码:)
关于javascript - .catch() 中返回的 promise 不能再次被 .catch'ed 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31045696/