I am working on a React Redux app. I have an action in which I am calling a utility function before dispatching the action.
import GetDataFromAPIs from "../utils/menu-creator";
export const UPDATE_DATA = 'UPDATE_DATA';
export const updateData = () => {
const newData = GetDataFromAPIs();
return{
type: UPDATE_DATA,
payload: newData
};
};
And, here is what GetDataFromAPIs.js is loosely doing:
import React from "react";
import axios from "axios";
const apiOneData = axios.get('/api/a');
const apiTwoData = axios.get('/api/b');
const GetDataFromAPIs = () => {
axios.all([apiOneData, apiTwoData]).then(axios.spread((...responses) => {
var firstDataSet = responses[0].data
var secondDataSet = responses[1].data;
var dataObject = [];
dataObject.push(firstDataSet);
dataObject.push(secondDataSet);
return dataObject;
})).catch(errors => {
console.log("There was an error.");
})
}
export default GetDataFromAPIs;
But when the application runs newData is set as undefined. I am trying to step through GetDataFromAPIs.js but it traces till axios.all and then breaks out of the function. I don't see any error in the console.
So what I am guessing is that action is dispatched before the two axios.all is resolved. I thought this would be synchronous (newData will get the data back and then the action will be dispatched). Am I doing this wrong?