waiting till code is done executing

waiting till code is done executing

I noticed that my code is not running in order that it is written.  Here is my code:

  1. $('#proceed').on('click', '#next3', function() {

    // CHeck values to see if account created

    // IF so, show next mod

    var accountCreated = 0;

    accountCreated = insertDataToDB(); // THIS RETURNS SUCCESS!

    if(accountCreated==1)

    {

    $("#mod3").fadeOut(1000, function(){

    $("#mod4").show();

    });

    }else{

    alert(accountCreated); // THIS RETURNS UNDEFINED

    }

    });

What is happening is that I get a popup of undefined (account failed to create), then a popup of success appears.