解析承诺的q.catch()等价物是什么?

时间:2014-08-19 15:58:34

标签: javascript parse-platform

在Q-promise库中,如果存在TypeError: Cannot read property 'blah' of undefined之类的随机问题,则可以通过Q.catch()块处理。

但是在Parse-promise库中,如果then()块内存在这样的问题,除了在每个then()块中放置一个try / catch之外,我目前看不到处理它的方法!

我错过了什么吗?什么是解析承诺的q.catch()等价物?

或者,如果没有每个then()块中的try / catch块,就无法捕获此类错误?

.then(function(data)){ try{ // do something with data that might throw an exception/error } catch(exception){ return Parse.Promose.error(exception); } })

1 个答案:

答案 0 :(得分:1)

根据spec,所有Promises / A +兼容库(包括Parse-promise库)中的.then()函数需要两次回调 - onFulfilledonRejected。如果第一个参数即。 onFulfilled抛出异常,承诺被拒绝,第二个参数将被调用。

换句话说,您的示例可以重写:

.then(function (data) { // do something with data that might throw an exception/error }, function (exception) { // handle the exception })

此外,Parse-promises(像许多承诺库一样)提供additional APIs来使这个更整洁。具体来说,如果拒绝承诺,您可以使用.fail()连接回调来调用。

相关问题