jquery wait till previous function call ends
<p>Please refer the below called function in our project.</p>

<pre><code>  var obj; // this will modified in A() function
function c()
{
   A();
   B(obj);
}

function A()
{

   for(i=0;i &lt; object.length; i++)
   {
     if(object.data.toString()=="remote data")
       processRemoteData();
     else
       processData();
    }

}

function  processRemoteData()
{
   $.ajax({
      url://remote url
      success:function(data)
      {
         // set this remote data in some object that will be passed in b function
      }
   });
}


function processData()
{
   //process data
}
</code></pre>

<p>am going to pass the obj (object) in B() function which will be modified in A() function or which will be set in A() function. but A() function  noting returned.</p>

<p>i want to wait A() function to fetch either remote data or normal data until B() function never going to start. because  based on the object modified in A() function am going to process in B() function.</p>

<p>but in my scenario A() function never waits until its fetch the data from remote URL, it starts B() function immediately.</p>

<p>i have tried like that </p>

<pre><code>$.when(A()).done(function()
{
  B();
});
</code></pre>

<p>this also not working and then i approached $.deferred  that means </p>

<pre><code>function A()
{
   var def= $.deferred();
   //process

   return def.promise();
}
</code></pre>

<p>but this kind of approach also not working fine for me since A() function  never waits until it completes the callback function and then start B()</p>

<p>Thanks,</p>

<p>Siva</p>
