Callback logic

2

I have a small problem and I would like an aid to understand and if possible an indication of the Srs to help this problem.

Let's start, I currently have a loop, I'm doing it in a for something like the code below:

for(var i = 0; i < 1500; i++){
 console.log("Valor "+i);
}

Inside this loop I'm calling a function that is updating some information, and in this same function I'm using callback at the end. (by my understanding the callback would be to type a pause in the code being that will only continue if there is the right callback?) The code is below

var Anonimo = {

  minhaFuncao: function(indice,callback){
    //faz algumas coisas

    //finaliza com callback
    callback(retornaAlgumaVariavel);
  }

}

So far beauty .. My complete code here is as follows:

var Anonimo = {

  minhaFuncao: function(indice,callback){
    //faz algumas coisas

    //finaliza com callback
    callback(retornaAlgumaVariavel);
  }

}


for(var i = 0; i < 1500; i++){
 console.log("Valor "+i);

 Anonimo.minhaFuncao(i, function(variavelRetorno){
  console.log("Entrou no indice "+i);
 });
} 

By my debug is having output correctly which would be, with each loop index is executing the callback .. Example

Valor 1
Entrou no indice 1
Valor 2
Entrou no indice 2
[....]

The problem is being in the following item ... when I call "myFunction" I pass the callback that is to execute something when finlizar the execution of it, in this case I am passing another function that also has a callback ... something like this:

var OutroAnonimo = {

  outraFuncao: function(callback){
    //faz algumas coisas

    //finaliza com callback
    callback();
  }

}

My complete code is as follows

for(var i = 0; i < 1500; i++){
 console.log("Valor "+i);

 Anonimo.minhaFuncao(i, function(variavelRetorno){
  console.log("Entrou no indice "+1);

  OutroAnonimo.outraFuncao(function(){
    console.log("Passou aqui...");
  });
 });
}

The error is in the following item, in the Loop, I go through the first and second console.log however when I call the "otherFunction" it is ignored, better explaining, the loop repeat 1500 times, in the debug exits the message "Value and Entered the prompt ... "but after it has passed 1500 times the messages" Passed here "appear.

My final doubt is, what did I do wrong? or what do I do to improve it? I need it to run as follows ...

The loop starts at 0, calls the "myFunction" function after it is finished calls the "otherFunction" after it has finished I go to position 1, 2, and so on.

    
asked by anonymous 30.05.2016 / 18:32

1 answer

2

This is the same problem this question / answer . That is, when this callback is called (once it is asynchronous) the loop is over and the i has changed in value however. So when you use i inside the callback the variable is no longer what you think. There's another related case .

In your case, you can solve it simply since you are passing i to the Anonimo.minhaFuncao method. Now if you use this i that was "consumed" by the function then you can pass it back on the callback like this:

var Anonimo = {

  minhaFuncao: function(indice, callback){
    //faz algumas coisas

    //finaliza com callback
    callback(indice, retornaAlgumaVariavel);
  }

}

and in the loop use:

for(var i = 0; i < 1500; i++){
 console.log("Valor " + i);

 Anonimo.minhaFuncao(i, function(indice, variavelRetorno){
   console.log("Entrou no indice " + indice);

If you want to force everything to happen sequentially you can do this:

function processar(arr) {
    if (arr.length == 0) return;
    var nr = arr.shift(); // e aqui encurta ao mesmo tempo a array
    console.log("Valor " + nr);
    Anonimo.minhaFuncao(nr, function(ind, variavelRetorno) {
        console.log("Entrou no indice " + ind);
        OutroAnonimo.outraFuncao(function() {
            console.log("Passou aqui... e terminou o indice", ind);
            processar(arr);
        });
    });
}

// para gerar o conteudo
var valores = [];
for (var i = 0; i < 15; i++) {
    valores.push(i);
}
processar(valores);

jsFiddle: link

    
30.05.2016 / 18:37