0

Please refer the below called function in our project.

  var obj; // this will 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 this remote data in some object that will be passed in b function
      }
   });
}


function processData()
{
   //process data
}

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.

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.

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

i have tried like that

$.when(A()).done(function()
{
  B();
});

this also not working and then i approached $.deferred that means

function A()
{
   var def= $.deferred();
   //process

   return def.promise();
}

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()

Thanks,

Siva

Ganesh Bora
  • 1,133
  • 9
  • 17
SivaRajini
  • 7,225
  • 21
  • 81
  • 128
  • why don't you call `B()` inside success function of `A()` – bipen May 10 '13 at 11:15
  • some time object data not will be remote data that time no need call B() function in success handler. but if i call B() function in success handler as well as outside A() function means it produce unexpected result. since we drawing SVG chart so it creates again – SivaRajini May 10 '13 at 11:20
  • please provide all information in question. – Ganesh Bora May 10 '13 at 11:27
  • i already given enough information thats why i put if condition here. – SivaRajini May 10 '13 at 11:32

2 Answers2

1

Try something like

function A() {
    var def = $.deferred();

    var collect = [];

    for (i = 0; i < 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 this remote data in some object that will be passed
            // in b function
        }
    });
}

function processData() {
    var def = $.deferred();
    // process data
    def.resolve();
    return def.promise();
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
-1

you can add "async: false" option to your $.ajax. Putting async to false tells jquery to use synchronous ajax query (by default, asynchronous ajax query is used)

Manitra
  • 111
  • 5