'Converting chained then statements into promise.all

I have a three-step process that needs to run synchronously. First, the app pulls information from one firebase collection, then using that result, pulls information from a second collection, then using that result, performs a function.

These are not running synchronously, so I'm missing the data assignments.

I believe the solution is working with promise.all, but I cannot structure it correctly:

Original Code:

 getUserInfo(user)
   .then(() => {
      //get data from database from result of getUserInfo
       getStyles(userInfo.value);
    })
   then(() => {
      //do things with data from database from result of 
      getStyles
       console.log(info);
        });

With Promise.all:

 let newUser = getUserInfo(user);
 let newStyles = getStyles(userInfo.value)

 Promise.all([newUser, newStyles]).then(() => {
   console.log("do things")
  }

The error is that the value newUser is still undefined.



Solution 1:[1]

It seems like you forgot to return anything from your promises so all you get is undefined.

Try like this.

getUserInfo(user)
   .then((userInfo) => {
      //get data from database from result of getUserInfo
      return getStyles(userInfo.value);
    })
   then((stylesInfo) => {
      //do things with data from database from result of getStyles
       console.log(stylesInfo);
   });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bao Huynh Lam