jquery wait till previous function call ends -


please refer below called function in our project.

  var obj; // modified in a() function function c() {    a();    b(obj); }  function a() {     for(i=0;i < object.length; i++)    {      if(object.data.tostring()=="remote data")        processremotedata();      else        processdata();     }  }  function  processremotedata() {    $.ajax({       url://remote url       success:function(data)       {          // set remote data in object passed in b function       }    }); }   function processdata() {    //process data } 

am going pass obj (object) in b() function modified in a() function or set in a() function. a() function noting returned.

i want wait a() function fetch either remote data or normal data until b() function never going start. because based on object modified in a() function going process in b() function.

but in scenario a() function never waits until fetch data remote url, starts b() function immediately.

i have tried

$.when(a()).done(function() {   b(); }); 

this not working , approached $.deferred means

function a() {    var def= $.deferred();    //process     return def.promise(); } 

but kind of approach not working fine me since a() function never waits until completes callback function , start b()

thanks,

siva

try like

function a() {     var def = $.deferred();      var collect = [];      (i = 0; < object.length; i++) {         if (object.data.tostring() == "remote data")             collect.push(processremotedata());         else             collect.push(processdata());     }      $.when(collect).then(function() {         def.resolve();     }, function() {         def.reject();     })      return def.promise(); } function processremotedata() {     return $.ajax({         url : '',         success : function(data) {             // set remote data in object passed             // in b function         }     }); }  function processdata() {     var def = $.deferred();     // process data     def.resolve();     return def.promise(); } 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -