0

I have the following functions...

    If(signInSet(sign_in_data)){
        do something... //appends data to a variable that is outputted after the if statement
    }
    else{
        do something else...      
    }    

Then...

   signInSet(sign_in_data){
      var equal = false;
      $.get('url', function(currentDate){
           $.each(sign_in_data, function compareDate(){
           if(this.date === currentDate){
                equal = true;
                return false;  //to end the each loop
           }
         });
          //at this point i can see and print out the value of equal
      });                

Im have some knowledge of the asynchronous nature of this function, what im wondering is how i can refactor my code to be able to see the value of equal so i can use it in my if statement. thanks in advance.

  • You really can't just refactor and keep the same flow, you should place the if statement inside the callback, that's the only place the data is avaiblable – adeneo Jun 07 '15 at 19:51
  • Why don't you put if statement into last callback. where equal is initialized? I believe you can't refactor the code without making if-check in the callback, even with Promises. – zmii Jun 07 '15 at 19:51
  • i tried but the the if statement is actually appending data (html info) to a variable that is output after the end of the function the if statement is in – Gavin Samuels Jun 07 '15 at 19:54
  • 1
    @GavinSamuels that's the thing with async programming - if _any_ part of the calculation depends on an asynchronously generated value then the _whole_ function needs to be asynchronous. And then everything that calls that, etc. – Alnitak Jun 07 '15 at 20:13
  • sounds like i need to convert it to a synchronous function then @Alnitak ? – Gavin Samuels Jun 07 '15 at 20:16
  • @GavinSamuels no, that's usually the wrong answer. Just go with the flow, perhaps learn how to use Promises, and embrace async. – Alnitak Jun 07 '15 at 21:31

0 Answers0