const test = () => {
let shown = { total: 0, thisWeek: 0, thisWeekData: [0, 0, 0, 0, 0, 0, 0] }
modalIds.map(async (data) => {
await fetch("api/statistics/private/get?modalId=" + data.modalId)
.then((response) => response.json())
.then((data) => {
if (data.success) {
shown = DataForAllShown(shown, data, "views")
}
})
})
return shown
}
The value of shown is always the same as the initial value, but the function DataForAllShown() gives it a new value. When trying to later access the variable it remains the same.
I understand that fetch is always async, but how can I update a variable outside of fetch and access it right after fetching?