0

It seems that since i have a "for loop" and multiple if/else scenarios, the callback never gets fired. When I remove the "for loop" it works fine. What am I doing wrong here? How do I force the callback to wait until all the for loops and conditionals are complete? Thanks!

var myArray = ["one","two","three","four","five","six","seven","eight","nine","ten"];

function myFunction(arg1,arg2,arg3,arg4,arg5,callbackFunction){

        if (arg1 == "arg1"){

            for (var i=0; i<=myArray.length; i++){

                    if(arg2 == "arg2"){

                        if(arg3 == "arg3"){    

                            if(arg4 == "arg4"){

                                 //some code

                            }else{ 
                                if(arg5 == "arg5"){
                                        //some code
                                    }else{
                                        //some code
                                    }
                                //some code
                            }
                        }else{ 
                           //some code
                        }
                    } 
                    else{
                        //some code
                    }
            }
        }else{
            //some code
        }  

        if (callbackFunction){
            callbackFunction();
        }
}

myFunction("arg1","arg2","arg3","arg4","arg5",function(){
    alert("done");                                            
});
sakeferret
  • 305
  • 1
  • 6
  • 15
  • 1
    I have checked the code. It works fine with and without **callbackFunction()**. Need more data to solve this. But I think somewhere in your if-else blocks inside for-loop you're returning. And for-loop is always synchronous – vijay krishna Oct 16 '18 at 16:31
  • 1
    If you're `return`ing in some if/else scenario, you might consider using `break`. So that the code comes out of the for loop instead of just exiting the function. – Sajal Preet Singh Oct 16 '18 at 16:35
  • Thanks to both of you. The code inside the conditionals is interfacing with Adobe After Effects and setting properties inside the app. So maybe that is what's forcing it to return. Will test the break idea. Thanks! – sakeferret Oct 16 '18 at 16:37
  • 2
    As long as the for loop is synchronous, you don't need to worry about anything. – briosheje Oct 16 '18 at 16:45

1 Answers1

0

Thanks to the comments above, i realized that since I was interfacing with After Effects, the program was forcing the function to return something and not allowing the callback to fire. I simply added this right after the "for" declaration and before all the conditionals:

if(i==myArray.length){break;}

and that solved it. Thanks guys!

R

sakeferret
  • 305
  • 1
  • 6
  • 15