Javascript 如何处理解析承诺错误,使它们不会';你不能继续执行代码吗?
假设我有一个Parse Cloud Code js函数,我想返回一个承诺,比如:Javascript 如何处理解析承诺错误,使它们不会';你不能继续执行代码吗?,javascript,parse-platform,promise,Javascript,Parse Platform,Promise,假设我有一个Parse Cloud Code js函数,我想返回一个承诺,比如: 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
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.reject(),但是当前正在执行的承诺链会发生什么情况呢?它是否会继续执行并继续执行D、E、F、G等。?那我就不能去一个我要去很多次的地方吗
我如何重构/重写它以更好地处理位置C、E和G的情况
适当地嵌套处理程序。如果一个处理程序应该只处理单个操作的解析,那么用链接它。然后直接在该操作的承诺上,而不是在链中的其他地方
在位置E中,您不会将处理程序附加到包含save调用的链,而只附加到oncompleted
(即未中止)分支中的承诺
否。关于.then()
的onRejection
处理程序的实际工作方式-在执行onCompleted
处理程序时不会调用它(此处调用doSoSoSomethinGunImportant()
)。在位置G中,您肯定知道then
调用失败之前的链中的某些内容-doSomethingCrucialyImportant()
,在我的简化代码段中
结合起来:
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;
});
}
我如何重构/重写它以更好地处理位置C、E和G的情况
适当地嵌套处理程序。如果一个处理程序应该只处理单个操作的解析,那么用链接它。然后直接在该操作的承诺上,而不是在链中的其他地方
在位置E中,您不会将处理程序附加到包含save调用的链,而只附加到oncompleted
(即未中止)分支中的承诺
否。关于.then()
的onRejection
处理程序的实际工作方式-在执行onCompleted
处理程序时不会调用它(此处调用doSoSoSomethinGunImportant()
)。在位置G中,您肯定知道then
调用失败之前的链中的某些内容-doSomethingCrucialyImportant()
,在我的简化代码段中
结合起来:
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;
});
}
您的dsPromise
就是一个例子。由于您没有拒绝承诺(而是从调用dsPromise.reject()
的错误处理程序返回undefined),您可能已经多次尝试解决它了。嗯,我明白您的意思了!我调用reject(),但不告诉promise块我已经在返回哪个执行。您的dsPromise
就是一个例子。由于您没有拒绝承诺(而是从调用dsPromise.reject()
的错误处理程序返回undefined),您可能已经多次尝试解决它了。嗯,我明白您的意思了!我调用reject(),但不要告诉promise块我已经在执行什么返回。感谢您的深入回答!2个问题:1。在您的示例中,如果我在H之后添加一个错误处理程序块,会怎么样?那么,//abort行和返回do…critical call都可以调用它吗?难道没有必要找出调用函数(错误)块的原因的代码情况吗?或者,在这种情况下,你应该总是做出承诺吗。如果承诺需要这么多嵌套,为什么要使用承诺而不是主干样式的成功/错误回调?感谢您指出我对.then()的onRejection处理程序的误解!阅读此文:帮助我回答问题#2为深入的答案做准备!2个问题:1。在您的示例中,如果我在H之后添加一个错误处理程序块,会怎么样?那么,//abort行和返回do…critical call都可以调用它吗?难道没有必要找出调用函数(错误)块的原因的代码情况吗?或者,在这种情况下,你应该总是做出承诺吗。如果承诺需要这么多嵌套,为什么要使用承诺而不是主干样式的成功/错误回调?感谢您指出我对.then()的onRejection处理程序的误解!阅读此文:帮助我回答问题2