7

I want to use Promise.all() to handle two promise object, but the second is inner a if expression. How to handle this situation?

It looks like this:

functionA(); 

if(true) {
    functionB(); 
}

functionA() and functionB() both return a promise object. In normal case, I could use

Promise.all([
    functionA(),
    functionB()
]).then(resule => {
    console.log(result[0]);  // result of functionA
    console.log(result[1]);  // result of functionB
})

But how to handle with the if expression? Should I wrap the if(true){functionB()} in a new Promise()?

Brick Yang
  • 5,197
  • 7
  • 31
  • 44

3 Answers3

11

Well, you can use ifs if you use promises as proxies for values, or you can nest the promise one level - personally - I prefer using them as the proxies they are. Allow me to explain:

var p1 = functionA();
var p2 = condition ? functionB() : Promise.resolve(); // or empty promise
Promise.all([p1, p2]).then(results => {
      // access results here, p2 is undefined if the condition did not hold
});

Or similarly:

var p1 = functionA();
var p2 = condition ? Promise.all([p1, functionB()]) : p1; 
p2.then(results => {
     // either array with both results or just p1's result.
});

Wrapping the conditional in a new Promise is explicit construction and should be avoided.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 260,410
  • 85
  • 489
  • 489
  • As `Promise.all()` will assimilate *values* as well as promises, that `Promise.resolve()` in the first solution could be a value, which would be an inexpensive way to inject a default value when `functionB()` is not called. [Demo](https://jsfiddle.net/fabup5aq/). – Roamer-1888 Dec 25 '15 at 20:32
  • I agree that calling Promise.resolve() is useless. Pass directly default value e.g. null or undefined. By doc it is valid to pass values instead of promises: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all – mikep Jul 30 '20 at 12:58
3
Promise.all([ cond==true ? p1 : '', p2])
Paul Roub
  • 35,848
  • 27
  • 79
  • 88
Pra2win
  • 53
  • 8
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/26170009) – MartenCatcher May 19 '20 at 13:30
  • 1
    I have searched for the same question and the above answer solved my problem hence posted. It may be helpful for them. Reputation matter here?. – Pra2win May 20 '20 at 10:34
3

In my case i had multiple conditions

 functionA(); 
 if(condition1) {
  functionX(); 
 }else if (condition2) {
  functionY(); 
 }....

so i did the following

const promises = [];
promises.push(functionA());
if (condition1) promises.push(functionX());
else if (condition2) promises.push(functionY());
......

Promise.all(promises).then((results) => console.log('results', results) );
thr
  • 51
  • 7