I have this JavaScript code:
function Static (subject, quantity){
this.subject = subject
this.quantity = quantity
}
async function get_statics(subjects) {
let total = []
subjects.forEach(async subject => {
let questions = []
let response = await fetch("/api/filter_question", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
User: {
id: 0,
subject: subject,
level: 0,
start_date: "1970-06-01T14:55:47.530641212",
end_date: "2999-04-14T14:55:47.530641212",
creator: 2,
verified: true
}
})
})
if (!response.ok) {
window.location = "/?status=questionModifyError"
}
else {
questions.push((await response.json()).length)
}
response = await fetch("/api/filter_question", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
User: {
id: 0,
subject: subject,
level: 1,
start_date: "1970-06-01T14:55:47.530641212",
end_date: "2999-04-14T14:55:47.530641212",
creator: 2,
verified: true
}
})
})
if (!response.ok) {
window.location = "/?status=questionModifyError"
}
else {
questions.push((await response.json()).length)
}
response = await fetch("/api/filter_question", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
User: {
id: 0,
subject: subject,
level: 2,
start_date: "1970-06-01T14:55:47.530641212",
end_date: "2999-04-14T14:55:47.530641212",
creator: 2,
verified: true
}
})
})
if (!response.ok) {
window.location = "/?status=questionModifyError"
}
else {
questions.push((await response.json()).length)
}
response = await fetch("/api/filter_question", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
User: {
id: 0,
subject: subject,
level: 3,
start_date: "1970-06-01T14:55:47.530641212",
end_date: "2999-04-14T14:55:47.530641212",
creator: 2,
verified: true
}
})
})
if (!response.ok) {
window.location = "/?status=questionModifyError"
}
else {
questions.push((await response.json()).length)
}
response = await fetch("/api/filter_question", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
User: {
id: 0,
subject: subject,
level: 4,
start_date: "1970-06-01T14:55:47.530641212",
end_date: "2999-04-14T14:55:47.530641212",
creator: 2,
verified: true
}
})
})
if (!response.ok) {
window.location = "/?status=questionModifyError"
}
else {
questions.push((await response.json()).length)
}
response = await fetch("/api/filter_question", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
User: {
id: 0,
subject: subject,
level: 5,
start_date: "1970-06-01T14:55:47.530641212",
end_date: "2999-04-14T14:55:47.530641212",
creator: 2,
verified: true
}
})
})
if (!response.ok) {
window.location = "/?status=questionModifyError"
}
else {
questions.push((await response.json()).length)
}
total.push(new Static(subject, questions))
})
return total
}
async function get_global_statics(subjects) {
let data = await get_statics(subjects)
console.log(data)
}
The use of this is to create an array of this kind [Static{“subject”, [0, 0, 0, 1}], …
The array is Ok it has got data, and also I can print it in the console. But when I try to access to an element or getting the length I get undefined.
I think that this happen because the value is shown before promise resolved.
Full code at text
I tried many things related to async. I expect to get some data when reading the array.