如何处理Parse promise错误,使它们不会'不要一直执行代码
How do you handle Parse promise errors so they don't keep executing code?
假设我有一个Parse Cloud Code js函数,我想返回一个promise,比如:
function doSomething(myObj, abortIfSaveFails) {
var dsPromise = new Parse.Promise();
myObj.set("name", "abc");
myObj.save().then(function(){
// location "A"
// great, it worked!
// don't want to actually do anything more in this block because
// we might want to do the same things even if save fails, and I
// don't want to repeat code
return Parse.Promise.as();
}, function(error){
// location "B"
// save failed, but we might want to keep going
if (abortIfSaveFails) {
// location "C": wish I could abort the whole promise chain here!
return Parse.Promise.error();
} else {
return Parse.Promise.as();
}
}).then(function(){
// location "D"
// at this point we're not sure if save succeeded but let's
// assume we don't need to know
return doSomethingCruciallyImportantAndReturnAPromise();
}, function(error){
// location "E":
// not sure if we got here because doSomethingCruciallyImportant...() errored or
// because myObj.save() errored.
// would be nice to abort the whole thing right now!
return Parse.Promise.error();
}).then(function(){
// location "F"
// at this point we know doSomethingElse... succeeded
return doSomethingUnimportantAndReturnAPromise();
}, function(error){
// location "G"
// not sure if we got here because doSomethingCruciallyImportant...() errored or
// because doSomethingUnimportant...() errored.
// If doSomethingCruciallyImportant...() succeeded but doSomethingUnimportant...()
// failed, I'd LIKE to do dsPromise.resolve()... but I can't resolve, because
// we might be in the process of aborting the function because myObj.save() rejected,
// or doSomethingCruciallyImportant rejected!
dsPromise.reject(); // might not be what I want to do!
}).then(function(){
// location "H"
// everything worked fine
dsPromise.resolve();
});
// location "I"
return dsPromise; // return the promise so `then` will wait
}
如何重构/重写它以更好地处理位置C、E和G的情况
我意识到我可以在C和E中使用dsPromise.rejectt(),但当前正在执行的promise链会发生什么?它不是会继续执行并转到D、E、F、G等吗。?那么我就不能到达一个我正在多次解析dsPromise的地方吗?
我如何重构/重写它以更好地处理位置C、E和G的情况?
适当嵌套处理程序。如果一个处理程序应该只处理一个操作的解析,那么直接在该操作的promise上用.then
将其链接起来,而不是在链中的其他地方。
在位置E中,您不会将处理程序附加到包含save调用的链,而只会附加到onFulfilled
(即未中止)分支中的promise。
doSomethingCruciallyImportant().then(function(){ // location "F" // at this point we know doSomethingCruciallyImportant succeeded return doSomethingUnimportant(); }, function(error){ // location "G" // not sure if we got here because doSomethingCruciallyImportant() errored // or because doSomethingUnimportant() errored. });
没有。阅读更多关于.then()
的onRejection
处理程序实际工作方式的信息-在执行onFulfilled
处理程序(此处调用doSomethingUnimportant()
)时,它不是调用的。在位置G中,您可以肯定地知道,在then
调用失败之前,链中的某个东西——在我的简化片段中是doSomethingCruciallyImportant()
。
组合:
function doSomething(myObj, abortIfSaveFails) {
myObj.set("name", "abc");
return myObj.save().then(null, // no onFulfilled block at all!
function(error){
if (abortIfSaveFails) {
return Parse.Promise.error(); // abort
} else {
return Parse.Promise.as(); // keep going regardless of the save fail
}
}).then(function() {
// either save succeeded or we don't care about it
return doSomethingCruciallyImportantAndReturnAPromise()
.then(function(){
// location "F"
// at this point we know doSomethingCruciallyImportant succeeded
return doSomethingUnimportantAndReturnAPromise().then(null, function(err) {
return Parse.Promise.as(); // we don't care if it errored
});
} //, function(error){
// location "G"
// doSomethingCruciallyImportant...() errored
// return Parse.Promise.error();
// }
);
}).then(function(result) {
// location "H"
// everything worked fine: Save succeeded (or we didn't care) and
// doSomethigCruciallyImportant() did as well
return result;
});
}
相关文章:
- setTimeout函数能否在其前面的代码执行之前激发
- 延迟代码执行,直到下载完成
- 奇怪的javascript代码执行顺序
- 将字符串作为一段 HTML/javascript 代码执行
- 使用AngularJS,如何在视图和控制器同步特定更改后触发代码执行
- onKeyUp 导致代码执行两次
- JQuery:将参数传递给插件以限制代码执行
- JavaScript代码执行时间
- 基本的JavaScript代码执行
- JavascriptCore:从本机代码执行JavaScript定义的回调函数
- 脚本标记的位置会影响代码执行
- 页面脚本未使用 Salesforce Web-to-Lead 代码执行
- GM_xmlhttpRequest 成功回调“onload”未为注入的代码执行
- 停止所有代码执行,直到 IFrame 完全加载
- 纯 Javascript 代码执行停止混合应用程序中的离子加载指示器
- Javascript代码执行顺序
- Javascript:在嵌套函数中使用条件语句停止代码执行
- Node.js代码执行顺序
- 什么触发JavaScript代码执行
- onreadystatechange导致php代码执行两次