I'm having problems with the Javascript execution method. I have this piece of code:
let array = [];
list.forEach((item_1) => {
let variable = 0;
fetch("data.json")
.then((resp) => resp.json())
.then((data) => {
data.forEach((item_2) => {
item_2.forEach((item_3) => {
item_3forEach((item_4) => {
if (condition) {
variable += 1;
}
});
});
});
});
array.push({a:variable, b: item_1});
});
I need the variable to be append only when the iterations have finished, however when I run the project, the variable append first (with initial value 0) and then do the iterations.
How could I make it append only when the process is finished?
EDIT:
Due I wasn't very clear before, I share the piece of code that I ask the question about
for (let c = -100; c < 501; c += 100) {
fetch(`data/votes_${c + 101}-${c + 200}.json`)
.then((resp) => resp.json())
.then((data_vot) => {
proy_etiq.forEach((eti_selec) => {
let cont_si_tot = 0;
data_vot.congresista.forEach((congresista) => {
congresista.proyecto.forEach((proyecto) => {
let cont_si = 0;
eti_selec.proyectos.forEach((proy_selec) => {
if (proyecto.id === proy_selec.id) {
cont_si += proyecto.voto.si;
proy_selec.votos[0] += cont_si;
}
});
cont_si_tot += cont_si;
});
});
flor_si.push({ Tema: eti_selec.nombre, abs: cont_si_tot });
});
});
}
The proy_etiq
is a list that I alredy have,
Copyright Notice:Content Author:「Juan Pablo Rocha」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/61765029/execute-an-instruction-after-finishing-another-in-javascript