我对 Promise 不太了解。我想在满足条件时触发我的函数failresponse
。 failresponse
在用户屏幕上显示文本错误。而且,如果可能的话,我想避免触发函数 getDataPDV
。我该如何解决呢? else
代码是正确的并且可以工作。
谢谢。
if (nomProduit,pdvType,classeProduit,sousClasseProduit == "") {
geoCode(location).then(function(locationFromApi) {
getDataPDV(locationFromApi, pdvType, nomProduit, classeProduit, sousClasseProduit)
.then(() => failResponse(res, requestBody.text));
});
} else {
geoCode(location).then(function(locationFromApi) {
getDataPDV(locationFromApi, pdvType, nomProduit, classeProduit, sousClasseProduit)
.then(function(data) {
return successResponsev2(res, data);
})
.catch(() => failResponse(res, requestBody.text));
});
}
最佳答案
这是我找到的,如果有帮助请告诉我
geoCode(location).then((locationFromApi) => {
getDataPDV(locationFromApi, pdvType, nomProduit, classeProduit, sousClasseProduit).then((data) => {
if(nomProduit && pdvType && classeProduit && sousClasseProduit){
failResponse(res, requestBody.text);
}
else{
successResponsev2(res, data);
}
};
}).catch(()=>{
failResponse(res, requestBody.text)
});
关于javascript - 在.then上添加if条件时如何使用promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58962516/