Execute an instruction after finishing another in Javascript
NickName:Juan Pablo Rocha Ask DateTime:2020-05-13T09:52:33

Execute an instruction after finishing another in Javascript

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

Answers
Abion47 2020-05-13T01:55:38

If you don't want the append to happen as part of the iteration, then move it out of the iteration.\n\nlet array = [];\nlet variable = 0;\n\nlist.forEach((item_1) => {\n ...\n});\n\narray.push(variable);\n",


More about “Execute an instruction after finishing another in Javascript” related questions

asp.net execute javascript function after finishing the button click

I have this asp.net button &lt;asp:Button ID="okButton" runat="server" Text="Okay" OnClick="okButton_Click" /&gt; this is the onButton_Click function protected void okButton_Click

Show Detail

Execute an instruction after finishing another in Javascript

I'm having problems with the Javascript execution method. I have this piece of code: let array = []; list.forEach((item_1) =&gt; { let variable = 0; fetch("data.json") .then((resp) =&gt; ...

Show Detail

Execute another instruction after executing JQuery jAlert plugin

I,m using jalert plugin. It works well but I'd like after showing the message box (dialog) being able to execute another instruction (namely I'm using it to check the empty textboxes in a form before

Show Detail

How ALU execute instruction in AMD GPU (VLIW)?

I wanna ask something about OpenCL programming. I understand that a quarter of wavefront can issue instruction for each cycle clock and it will need four cycle clock to call a wavefront. To finis...

Show Detail

Execute an instruction later

I would like to give an instruction as a parameter: execute_at_frame(int frame_number, &lt;instruction&gt;) { for(f = 1 ; f &lt; F_MAX ; f++) { /* other instructions */ if ...

Show Detail

Calling javascript function after finishing bean method

I want to execute javascript function after bean actionListener.I am using JSF 1.2, richfaces 3.3.In my case I want to display a chart after finishing bean's method.My table tabis filling but the

Show Detail

Starting one task after finishing another

In a .vbs i have something like Dim sh Set sh = WScript.CreateObject("WScript.Shell") 'run conf sh.run "cmd /K php -c php.ini -f some_path\runer\run.php &amp; pause",0,false 'Naviga

Show Detail

how to execute a function after finishing the first one

i have this input : &lt;button type="button" title="test" class="button btn-cart" onclick="productAddToCartForm.submit(this); redirectToCart();"&gt;OK&lt;/button&gt; i want to execu

Show Detail

GDB execute next instruction after watchpoint is hit

I have a function of the following form: void foo(){ int *a = //... *a = 1; //some actions *a = 2; //some actions *a = 3; //some actions //etc... } I want to set a

Show Detail

How to execute something after finishing map function?

I want to execute something after finishing the map function. I thought we could use the callback function as follows. But it doesn't exist, so it didn't work: this.data.map((v, i) =&gt; { co...

Show Detail