JavaScript How to nest multiple Promise.all

I have multiple arrays of promises

Each array is put inside a Promise.all()

The then() of each Promise.all() adds data to a tempObject

I need to set the tempObject to state after then() of all Promise.all() are done executing.

What is the best approach (clean and readable code) to achieve this?

below is my code

callSomeApis(parameter){
  let tempArray1 = [];
  let tempArray2 = [];
  this.props.dispatch(actions.callApi1(parameter)).then(callApi1Result =>{
    let callApi1ResultArray = callApi1Result.data.data;
    let PromiseArr1 = callApi1ResultArray.map((callApi1ResultArrayItem) => {
      return this.props.dispatch(actions.callApi2(callApi1ResultArrayItem.itemId));
    });
    let PromiseArr2 = callApi1ResultArray.map((callApi1ResultArrayItem) => {
      return this.props.dispatch(actions.callApi3(callApi1ResultArrayItem.itemId,parameter));
    });
    let PromiseArr3 = callApi1ResultArray.map((callApi1ResultArrayItem) => {
      return this.props.dispatch(actions.callApi4(callApi1ResultArrayItem.itemId));
    });
    Promise.all(PromiseArr1).then((callApi2Result) => {
      callApi2Result.map((callApi2ResultItem,index) => {
        callApi1ResultArray[index].api2Details = callApi2ResultItem.data.data[0];
        tempArray2.push({id: callApi2ResultItem.data.data[0].id, text: callApi2ResultItem.data.data[0].text});
      });
      this.setState(prevState => {
        return{
          stateKey1: {
            ...prevState.stateKey1,
            innerStateKey1: {
              ...prevState.stateKey1.innerStateKey1,
              list: tempArray2
            }
          }
        }
      });
    });
    Promise.all(PromiseArr2).then((callApi3Result) => {
      callApi3Result.map((callApi3ResultItem, index) => {
        callApi1ResultArray[index].api3Result = callApi3ResultItem.data.data;
      });
    });
    Promise.all(PromiseArr3).then(callApi4Result => {
      callApi4Result.map((callApi4ResultItem,index) => {
        callApi1ResultArray[index].api4Result =  callApi4ResultItem.data.data;
      });
    });
    /**need to call this after the thens of  PromiseArr1, PromiseArr2 and PromiseArr3 are done executing*/
    this.setState({
      stateKey2:callApi1ResultArray
    })
    /** */
  })
}
Answer:1

If you're able, you can also use async/await

async function (...) {
  const r1 = await Promise.all(...);
  // do something with r1
  const r2 = await Promise.all(...);
  // ...
}

and so on. Make sure you're using Promise.all on actions that can be parallelized (that also return a Promise), though.

I find that async/await really cleans up some nasty Promise chains/nesting and helps a lot with readability.

Answer:2

Promise.all returns a promise so you can do:

const p1 = Promise.all(PromiseArr1).then(...);
const p2 = Promise.all(PromiseArr2).then(...);
const p3 = ...
Promise.all([p1, p2, ...]).then(...);

If all your promises are very similar you can clean it up by creating an array and mapping it to the promises.

Answer:3

Is there a way to do both ( toLowerCase and includes ) in one go like below? let x = this.state.outreach.arival.toLowerCase(includes("likely"))

Is there a way to do both ( toLowerCase and includes ) in one go like below? let x = this.state.outreach.arival.toLowerCase(includes("likely"))

  1. string lowercase contains
  2. string contains lowercase java
  3. string contains lowercase c#
  4. string contains lowercase javascript

I would like to run a website developed with node.js in local. I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js ) Also, I guess I have to ...

I would like to run a website developed with node.js in local. I already installed node.js but when I lauch a .js file on my terminal, nothing happen ( $ node file.js ) Also, I guess I have to ...

  1. website developed with
  2. website developed with django
  3. website developed with angular
  4. website developed with php
  5. website developed with node.js
  6. website developed in python
  7. website developed by footer
  8. website developed in which language
  9. website developed in react
  10. website developed in which technology
  11. website developed in wordpress
  12. website developed in angularjs
  13. website developed in java
  14. website developed in angular 2
  15. website developed in magento
  16. website developed in asp.net
  17. website developed in vuejs
  18. website developed in ruby on rails
  19. website developed in html
  20. website developed by tcs

I'm trying to determine whether a year is a leap year or not. I'm not sure where i'm missing something because this code is meant to determine that. Thanks for your help. let Year = (year) => { ...

I'm trying to determine whether a year is a leap year or not. I'm not sure where i'm missing something because this code is meant to determine that. Thanks for your help. let Year = (year) => { ...

  1. determine whether the year

I have an interactive d3 map running (http://atlas.niu.edu/afd/) that is based on a topojson file. The structure of the topojson is as follows: I'd like to display the CWA and City on mouseover and ...

I have an interactive d3 map running (http://atlas.niu.edu/afd/) that is based on a topojson file. The structure of the topojson is as follows: I'd like to display the CWA and City on mouseover and ...