Order of execution in jQuery : <p>I have the following code in a website:</p>

<pre><code>              alert('step 1');
              // save token if app user: 
              if (tokenViaApp !== '' ) {
                  alert('step 2')
                  $.ajax({ 
                      type: "GET", 
                      url: "/includes/notifications/", 
                      data: {
                          t: tokenViaApp    
                      }, 
                      success: function(msg) {  
                           localStorage.token_origin = 'app';
                           alert('step 3')
                      }
                    });
              }
              alert('step 4')
</code></pre>

<p>If the <code>if()</code> passes, then i would think the alert should be in this order:</p>

<pre><code>step 1
step 2
step 3
step 4
</code></pre>

<p>but instead I get:</p>

<pre><code>step 1
step 2
step 4
step 3
</code></pre>

<p>Why is this the case? and how can this be changed to process correctly? </p>
