javascript - Alternative to "async: false" for successive AJAX calls -


i using 2 ajax get requests grab data 2 different sources.
in each request parse response data, create temporary array relevant data each item, , push temporary array master array.
however, works if include "async: false" in ajax request, deprecated.
there alternative way write code? (note: although using 2 ajax requests in code snippet, need use total of 4 in project working on).

function get_results() {   $(document).ready(function() {     var master_array = [];     $.ajax({       type: "get",       url: "http//:www.source1.com",       datatype: "xml",       async: false,       success: function(xml) {         $(xml).find('product').each(function() {           var average = $(this).find('average').text();           var price = $(this).find('price').text();           var name = $(this).find('name').text();           var url = $(this).find('url').text();           var image = $(this).find('image').text();           master_array.push([average, price, name, url, image]);         });       }     });     $.ajax({       type: "get",       url: "http//:www.source2.com",       datatype: "xml",       async: false,       success: function(xml) {         $(xml).find('product').each(function() {           var average = $(this).find('average').text();           var price = $(this).find('price').text();           var name = $(this).find('name').text();           var url = $(this).find('url').text();           var image = $(this).find('image').text();           master_array.push([average, price, name, url, image]);         });       }     });   }); } 

you can achieve using jquery $.promise object.
when return $.ajax it's has built-in methods, such $.done() , $.when.

in scenario want execute ajax function after first ajax request done.

so create 2 variables represent 2 ajax requests have in code.
mentioned earlier when return ajax request can use $.promise object , enjoy advantages has, such $.done() function.

    function get_results() {   $(document).ready(function() {     var master_array = [];     var myfirstfunction = function() {       return $.ajax({       type: "get",       url: "http//:www.source1.com",       datatype: "xml",       success: function(xml) {         $(xml).find('product').each(function() {           var average = $(this).find('average').text();           var price = $(this).find('price').text();           var name = $(this).find('name').text();           var url = $(this).find('url').text();           var image = $(this).find('image').text();           master_array.push([average, price, name, url, image]);         });       }     });    };      var mysecondfunction = function(){       return $.ajax({       type: "get",       url: "http//:www.source2.com",       datatype: "xml",       success: function(xml) {         $(xml).find('product').each(function() {           var average = $(this).find('average').text();           var price = $(this).find('price').text();           var name = $(this).find('name').text();           var url = $(this).find('url').text();           var image = $(this).find('image').text();           master_array.push([average, price, name, url, image]);         });       }     });    }; //we use after assignment of variables because if have put before them got undefined, can read more here:[hoisting][4].       myfirstfunction().done(mysecondfunction);      });     } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -