在.then上添加if条件时如何使用promise?

我对诺言并不了解。我想在满足条件时触发我的函数failresponsefailresponse在用户屏幕上显示文本错误。并且,如果可能的话,我想避免触发函数getDataPDV。我应该如何解决? else代码正确无误。

谢谢。

    if (nomProduit,pdvType,classeProduit,sousClasseProduit == "") {
        geoCode(location).then(function(locationFromApi) {
          getDataPDV(locationFromApi,nomProduit,sousClasseProduit)
          .then(() => failResponse(res,requestBody.text));
        });
    } else {      
        geoCode(location).then(function(locationFromApi) {
        getDataPDV(locationFromApi,sousClasseProduit)
        .then(function(data) {
          return successResponsev2(res,data);
        })
        .catch(() => failResponse(res,requestBody.text));
      });
    }
fluid120 回答:在.then上添加if条件时如何使用promise?

这是我发现的,请告诉我是否有帮助

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)
});
,

之后,您似乎没有返回任何数据 第二个承诺已解决

为避免触发该功能,您可以尝试使用此功能

功能(){ getDataPDV(locationFromApi,pdvType,nomProduit,classeProduit,sousClasseProduit }

这将防止自动调用getDataPDV。

本文链接:https://www.f2er.com/3062759.html

大家都在问