I have something as follows:
const [channelAndReadsArray, setChannelAndReadsArray] = useState()
var channelAndReads = []
const requests = channels.map((currentChannel) => {
axios.get(serverRestAddress...
.then((result) => {
var element = {}
element.channel = currentChannel;
element.reads = result;
channelAndReads.push(element);
})
})
Promise.all(requests).then(() => {
setChannelAndReadsArray(channelAndReads)
});
...
if (!channelAndReadsArray) {
return null
})
channelAndReadsArray.map((channelAndReads) => {
console.log(channelAndReads)
})
This is giving me null values in the console log. I am not sure what is wrong here
.map()callbackaxioscall is async. The loop would finish before even a single http call succeeds and so your console.log will not have anything. You need to wait until all the api is succeeded.There will be no value in channelAndRead coz nothing would be pushed by the timeconsole.logexecutes.axioscall is async..." - No, because TO "waits" for the results withPromise.all(requests). The problem is the missingreturnin.map()