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

标签 javascript es6-promise

我对 Promise 不太了解。我想在满足条件时触发我的函数failresponsefailresponse 在用户屏幕上显示文本错误。而且,如果可能的话,我想避免触发函数 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/

相关文章:

javascript - 使用 javascript 分割文本而不丢失数据

javascript - 将多组参数分配给 jQuery 函数

javascript - 为什么javascript不等待和forEach并执行下一行

javascript - 从网格索引中获取行号

javascript - 为两个不同的动态生成的列表选择全部按钮

javascript - Observable toPromise() 获取通过 next() 发送的所有数据

javascript - 当错误可能未包含在 promise 中时,我如何使用 promise 来捕获错误?

javascript - 在 Promise 链的中间传递一个额外的参数

javascript - Promise 链接成功和拒绝功能

javascript - AngularJS 指令实现中的语法错误