Serhio g. Lazin
Serhio g. Lazin

Reputation: 9642

Synchronize Json data from server responce

I have two async responces from Ios server:

serviceResponce1 = function(response, request){
   result1 = Jquery.parseJson(request);
}

serviceResponce2 = function(response, request){
   result2 = Jquery.parseJson(request);
}

Than I need filter these two results:

filterArray = result1.filter(function(item){
   return result2.indexOf(item.Id) !== -1;
}

but they coming asynchronous and filter doesn't works. (when I hard code data, it works) How to synchronize them?

Upvotes: 1

Views: 60

Answers (1)

Ponpon32
Ponpon32

Reputation: 2200

The easiest way to achieve that is to make the second request inside the handler of the first request and then handle the response:

request1(function(res1) { 
 request2(function(res2) { 
  filter(res1, res2); 
 }) 
});

The second way is to use deferred object, if you using jQuery (latest versions) then you can use:

$.when(request1, request2).done(filter)

Upvotes: 2

Related Questions