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.