I have 2 promises that takes in input 2 differents parameters:
These promises runs in parallel inside a Promise.all():
Sometimes, depending on the value of var1 or var2, I just need one of them to be executed. For example, if var1 is null, I just want
Or, if var2 is null:
Is there an elegant way to code this situation without if, else branches?
it can be done with dynamically create promise.all array argument.
var promises=; if(var1==null) promise.push(promise2(var2)); if(var2==null) promise.push(promise1(var1)); Promise.all(promises);
Why do you think
if .. else is not elegant? In many situation it's the most readable code and thus IMHO is better than some "well-constructed" oneliner, which you won't understand anymore the next time you are reading it.
I personally would make an array and push the various promises into that array based on the conditions. And then finally make a
Promise.all with this array. IMO this is the most readable and elegant way to do this.
let promises = ; if (val1) promises.push(promise1(val1)); if (val2) promises.push(promise1(val2)); Promise.all(promises);