Reputation: 9
how to call conditional promise without nested promise and execute the rest of the code irrespective whether condition statisfy or not
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
})
Upvotes: 1
Views: 488
Reputation: 708176
This type of logic is simplest with async/await
because you can write more traditional sequential code flow logic.
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;
}
Without async/await
you do have to do some nesting, but only for the conditional:
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();
});
}
Upvotes: 2