inheritance失败后继续
我有一个承诺可能会失败的情况,但我希望能够处理,并继续下一步。 我试图从失败捕获中返回一个成功的承诺,但它提供了一个关于没有方法设置的返回对象的错误。 这可能吗? 我将如何去做呢?
Parse.Promise.as(1).then(function() { if (user.get('vendor')) { //fetch returns a promise return user.get('vendor').fetch(); } return new Vendor(); }).fail(function() { //this will be called if the fetch fails, in that case, just return new Vendor(); return Parse.Promise.as(function() { //this will be a valid promise so should hopefully return to the next then, but it doesn't work return new Vendor(); }); }).then(function(result) { vendor = result; //continue with stuff }).fail(function(error) { res.json(400, { "result": false, "error": error }); });
编辑:
我试图改变它:
Parse.Promise.as(1).then(function() { if (user.get('vendor')) { return user.get('vendor').fetch(); } return new Vendor(); }).then(null, function() { //if the fetch fails, this will return a successful Promise with Vendor object console.log("failed fetch"); return new Vendor(); }).then(function(result) { console.log("vendor retrieved"); }).then(null, function(error) { console.log('error'); });
但是logging:提取错误失败
这只是如何parsing,或者是其他的错误?
EDIT2:
似乎工作,如果我改变了
return new Vendor();
行到
return Parse.Promise.as(1).then(function() { return new Vendor(); });
(编辑)或者这个:
return Parse.Promise.as(new Vendor());
就像你说的,从exception中恢复是可能的,承诺:
try{ mightThrow() } catch (e){ // handle } thisWillRunRegardless();
或与parsing承诺:
Promise.as(1).then(function(){ mightThrow(); }).then(null,function(e){ // handle }).then(function(){ thisWillRunRegardless(); });
与其他承诺库,它可能看起来像:
Promise.try(function(){ mightThrow(); }).catch(function(){ //handle ]).then(thisWillRunRegardless);
上面的代码的问题是.fail
。 由于parse.com承诺是jQuery投诉 – 他们的失败方法行为像jQuery的。 它添加了一个失败处理程序,并返回相同的承诺 。
不知道为什么他们这样做,但哦。 你需要改变.fail(function(){
to .then(null,function(){...
。第二个参数。然后.then
是拒绝处理程序。