I need to loop through the api call based on the length and then need to store the response inside the state.
The problem is that when I use Promises all it takes 1 minute 30 seconds to resolve the array of promises which I can't afford, so I need to make it synchronous so it takes around 25 seconds for one promise to be resolved and then the loop should continue until it reaches the length...
const [data, Setdata] = useState([]);
const length = 10;
let users = [];
for (i = 0; i < length; i++) {
users.push(axios.get('/user/' + ${i}))
};
Promise.all(users)
.then(
response => response.map(
res => Setdata(res.data)
)
);
If your goal is to only pull one user at a time, could be because you're sending over a list and the server can't handle more than one request at a time... Whatever the reason for doing one request at a time, you could do something like this.
const [data, Setdata] = useState([]);
let userIds = [1,2,3,4,5,6,7,8,9,10];
let users = [];
while (userIds.length !== 0) {
id = userIds.pop();
await axios.get('/user/' + ${id})
.then(function (response) {
users.push(response);
});
}
response => response.map(
res => Setdata(res.data)
)
I have not tested this, just threw it together but it might get you on the right track.