Javascript 解析云代码承诺错误处理
我对JS有些陌生,对承诺也很陌生 假设我有一系列承诺:Javascript 解析云代码承诺错误处理,javascript,parse-platform,promise,parse-cloud-code,Javascript,Parse Platform,Promise,Parse Cloud Code,我对JS有些陌生,对承诺也很陌生 假设我有一系列承诺: //var challenge is created before all this isUserInCooldown().then( function(hoursRemaining) { if (hoursRemaining > 0) { return Parse.Promise.error("You can't challenge 'cuz you're on cooldown.");
//var challenge is created before all this
isUserInCooldown().then( function(hoursRemaining)
{
if (hoursRemaining > 0)
{
return Parse.Promise.error("You can't challenge 'cuz you're on cooldown.");
}
var objectsToSave = [];
//DO SOME STUFF TO THE OBJECTS
return Parse.Object.saveAll(objectsToSave);
}).then( function(list)
{
//DO SOME STUFF TO CHALLENGE
return challenge.save(null);
}).then( function(challenge)
{
return Parse.Promise.as(challenge);
},
function(error)
{
if (theSaveAllFailed) { return "Couldn't save all"; }
if (theSaveFailed) { return "Couldn't save the challenge"; }
//etc.
});
我正在重构一组使用回调的代码,每个error:function(error){}
返回一条自定义错误消息。我希望能够传递一个自定义的错误消息,这取决于链的故障位置
我想这和我的工作有关,但我还不知道怎么做
我想知道:
1:我怎样才能像我想要的那样返回自定义错误消息
2:我是否正确地使用了承诺(基于您在这里看到的内容?)
谢谢
.fail
添加怀疑的处理程序
应包括isuserincoldown
检查,以免污染其他功能李>hoursresleving>0
- 如果
不依赖于challenge.save
,则同时触发它们,并使用objectsToSave
将更快Parse.Promise.when
- 您从不根据云函数的要求调用
或response.success
李>response.error
.then(function(challenge){
return Parse.Promise.as(challenge);
}).then(function(challenge){
return Parse.Promise.as(challenge);
}).then(function(challenge){
return Parse.Promise.as(challenge);
});
啊,好多了:
isUserInCooldown().then( function() {
return Parse.Object.saveAll(objectsToSave).fail(function(){
return "Couldn't save all";
});
}).then( function(){
return challenge.save().fail(function(){
return "Couldn't save the challenge";
});
}).then(response.success, response.error)
或者,如果您没有使用云功能:
.fail(function(error) {
console.log(error); //logs "Couldn't save all" or "Couldn't save the challenge"
});