multiple api calls through promise update1: 

- I need to make two different api calls.
- from the result of first api call I am getting id in the variable firstAPIid, 
https://reqres.in/api/users?page=2
- I need to pass this id firstAPIid to the second api call.


can you review my code.
I did multiple api calls through promise.
right way is through promise or async or call back.
providing my code snippet and sandbox below.


https://codesandbox.io/s/redux-async-actions-xjdo7



&lt;FetchButton
          onFetchClick={() =&gt; {
            store.dispatch(dispatchFunc =&gt; {
              dispatchFunc({ type: "FETCH_DATA_START" });
              axios
                .get("https://reqres.in/api/users?page=2")
                // axios.get('https://jsonplaceholder.typicode.com/posts')
                .then(response =&gt; {
                  console.log("response.data.data----&gt;", response.data.data);
                  console.log(
                    "response.data.data[0].id----&gt;",
                    response.data.data[0].id
                  );
                  //First of all we'll create the number of requestes base on the previous Response
                  const promises = response.data.data.reduce(
                    (previousValue, { id }) =&gt; {
                      previousValue.push(
                        axios.get(
                          `https://jsonplaceholder.typicode.com/comments?postId=${id}`
                        )
                      );
                      console.log(
                        " promises previousValue----&gt;",
                        previousValue
                      );

                      return previousValue;
                    },
                    []
                  );

                  console.log("promises----&gt;", promises);

                  //We use the built in function to fetch the data
                  axios.all(promises).then(responses =&gt; {
                    //Here you have all responses processed
                    const emailsMapped = responses.reduce(
                      (previousValue, { data }) =&gt; {
                        const emails = data.map(({ email }) =&gt; email);
                        previousValue.push(...emails);
                        return previousValue;
                      },
                      []
                    );
                    //You send the emails you want
                    dispatchFunc({
                      type: "RECEIVED_DATA",
                      payload: emailsMapped
                    });
                    console.log(emailsMapped);
                  });
                })
                .catch(err =&gt; {
                  dispatchFunc({ type: "FETCH_DATA_ERROR", payload: err });
                });
            });
          }}
        /&gt;
First of all, putting all your code in the props of your JSX is making it unreadable. Extract this into a function, and have your JSX reference that instead.

Next, for debugging, I would recommend using breakpoints to debug code. console.log() is fine for a quick peek, but it introduces a lot of noise in the code.

Also, when you expand a logged object in the console, you see the object's structure at the time of expanding, not at the time of logging. The object's contents may have already changed between the time it was logged and when you expanded it. If you're not aware of this quirk, you'll easily think your code is broken.

For promises, you're really just mapping. Use array.map() instead of array.reduce(). For emailsMapped, which is also just a mapping operation, since you're adding multiple items, use array.flatMap() for that.

Lastly, you can modify your code so that you can chain the promises instead of nesting them. You can return the promise of axios.all() to chain it to your axios.get().

store.dispatch(dispatchFunc =&gt; {
  dispatchFunc({ type: "FETCH_DATA_START" });
  axios
    .get("https://reqres.in/api/users?page=2")
    .then(response =&gt; {
      const promises = response.data.data.map(({ id }) =&gt; {
        return axios.get(`https://.../comments?postId=${id}`)
      })

      return axios.all(promises)
    })
    .then(responses =&gt; {
      const emailsMapped = responses.flatMap(({ data }) =&gt; {
        return data.map(({ email }) =&gt; email)
      })

      dispatchFunc({
        type: "RECEIVED_DATA",
        payload: emailsMapped
      })
    })
    .catch(err =&gt; {
      dispatchFunc({
        type: "FETCH_DATA_ERROR",
        payload: err
      })
    })
})


Now that we've established a more linear flow via chaining, we can take this a bit further and convert it to an async function so that we can use await:

// Note the async here
store.dispatch(async dispatchFunc =&gt; {
  dispatchFunc({ type: "FETCH_DATA_START" });

  try {
    // Note the various await before each asynchronous function call

    const response = await axios.get("https://reqres.in/api/users?page=2")

    const promises = response.data.data.map(({ id }) =&gt; {
      return axios.get(`https://.../comments?postId=${id}`)
    })

    const responses = await axios.all(promises)

    const emailsMapped = responses.flatMap(({ data }) =&gt; {
      return data.map(({ email }) =&gt; email)
    })

    dispatchFunc({
      type: "RECEIVED_DATA",
      payload: emailsMapped
    })
  } catch (err) {
    dispatchFunc({
      type: "FETCH_DATA_ERROR",
      payload: err
    })
  }
})