I am trying to create synchronous looping using fetch in react native.
This is the reference I took from here:
apiCallConfigFetch = async () => {
var item = 0
global.configDetails = []
var temp = {}
console.log(global.userLocationID)
for (item = 0; item < global.userLocationID.length; item++) {
const response = await fetch(global.locationConfigUrl, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
locationID: global.userLocationID[item]
}),
})
const data = await response.json()
if (data == '') {
} else {
temp['locationID'] = data['id']
temp['name'] = data['name']
temp['configuration'] = data['configuration']
global.configDetails.push(temp)
console.log(global.configDetails)
}
}
}
This is my code
I want to replicate synchronous behaviour. The array in which I am pushing the data is taking the last element as repeat due to function's async behaviour. I have tried async but it's not working. Any help will be much appreciated. I have used the solutions in the answer but it's not working.