在 .then 上添加 if 条件时如何使用 promise?
How to use promise when adding if condition on .then?
我不太了解承诺。我想在满足条件时触发我的函数 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));
});
}
之后您似乎没有返回任何数据
第二个承诺已解决
为避免触发该功能,您可以尝试这样设置
函数(){
getDataPDV(locationFromApi, pdvType, nomProduit, classeProduit, sousClasseProduit
}
这将阻止自动调用 getDataPDV。
这是我找到的,如果有帮助请告诉我
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)
});
我不太了解承诺。我想在满足条件时触发我的函数 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));
});
}
之后您似乎没有返回任何数据 第二个承诺已解决
为避免触发该功能,您可以尝试这样设置
函数(){ getDataPDV(locationFromApi, pdvType, nomProduit, classeProduit, sousClasseProduit }
这将阻止自动调用 getDataPDV。
这是我找到的,如果有帮助请告诉我
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)
});