Wait until "if" statement is done?

Wait until "if" statement is done?

Is it that possible?

  1. if($("#results").hasClass("busy"))
  2.             {
  3.                 $("#gradiente").fadeOut(800, function() {
  4.                     $(this).remove();
  5.                 });
  6.             }
  7.            
  8.             $("#results").append("<div style='height: "+ altura +"px; width: "+ anchura +"px;' id='gradiente' />");
  9.            
  10.                 for(i=0; i<=altura; i++) {
  11.                
  12.                     var r = desde_rgb[0]-(step_r*i);
  13.                     var g = desde_rgb[1]-(step_g*i);
  14.                     var b = desde_rgb[2]-(step_b*i);
  15.                    
  16.                     $("#gradiente").append("<span class='grano' style='background: rgb("+ Math.round(r) +", "+ Math.round(g) +", "+ Math.round(b) +");' />");
  17.                 }
  18.                
  19.                     $("#gradiente").css({'opacity': '0'});
  20.                     $("#results").animate({"height": $("#gradiente").height(), "padding": "15px"}, 1800, function() {
  21.                         $("#gradiente").fadeTo(800, 1);
  22.                     }).addClass("busy");

I want that because, in the above code, the for loop doesn't wait to the if statement is finished and overrides the .fadeTo function. I don't know, i think that what i'am looking for is some kind of "manual callback" :P

Is it understandable? hahaha