Javascript 节点js中的条件承诺

Javascript 节点js中的条件承诺,javascript,node.js,Javascript,Node.js,如何在没有嵌套承诺的情况下调用条件承诺并执行其余代码,而不管条件statisfy与否 findAllByServiceProviderLocationId(serviceProviderLocationId, supplierId) .then(result => { // 1. set all the default values ChargesAdminController._setDefaultValues(result); //Bas

如何在没有嵌套承诺的情况下调用条件承诺并执行其余代码,而不管条件statisfy与否

        findAllByServiceProviderLocationId(serviceProviderLocationId, supplierId)
    .then(result => {
    // 1. set all the default values
    ChargesAdminController._setDefaultValues(result);
    //Based on some condition in result - i need to call new promise
    //If condition satisfy, do promise operation and continue executing. is there better way to do apart from nested promise`enter code here`
    //Ex:
    if(result.checkPricing){
        DBConnection.getPricing(result)
     }
        //some operations on result object before sending response - All these operations should run only after the conditional promise is fulfilled 
    })

使用
async/await
这种逻辑最简单,因为您可以编写更传统的顺序代码流逻辑

async function myFunc() {
    let result = await someFunc1();
    if (result.whatever === something) {
         // asynchronous operation inside the if statement
         await someFunc2();
    }
    // code here that gets executed regardless of the above if
    let someOtherResult = await someFunc3();
    return someResult;
}
如果没有
async/await
,则必须进行一些嵌套,但仅限于条件:

function myFunc() {
    return someFunc1().then(result => {
        if (result.whatever === something) {
            // asynchronous operation inside the if statement
            return someFunc2();
        } else {
            return somethingElse;
        }
    }).then(thing => {
        // code here that gets executed regardless of the above if statement
        return someFunc3();
    });
}

谢谢你澄清这一点。在if block中的一个小问题是,我们返回的承诺将在下一个then block中处理。有没有可能只在其他地方发送对象而不是承诺?@PhaniKumar-我不知道“只在其他地方发送对象而不是承诺”是什么意思?我的意思是只在其他地方返回空白的对象。。相似的返回结果;在其他地方作为承诺在如果块像。。Dbconnection.getPrices()。我们需要用蓝鸟吗。Resolve()在else?@PhaniKumar中-参见我的第二个代码示例。在
.then()
处理程序中,可以在
if
中返回一个承诺,在
else
中返回一个值。这正是我的例子所做的。在
else
中,该值成为链的解析值。在
if
中,您返回的承诺的最终解析值将成为链的解析值。两者都是完全允许的。