Panais Panais - 3 years ago 235
Ajax Question

jQuery Promise for Ajax call

I think/hope I am missing something regarding the promise programming paradigm. I run the following code on jQuery because I want to get the data from URL_1 and then (on success) to get the data2 from URL_2. The other variables come the the context surrounding this piece of code.

However, what I get is the data of the URL_1 twice!!

url: URL_1,
type: "GET",
async: false,
cache: false
}).then(function (data) {
myObj = process(otherObj, data, URL_1);
return $.ajax({
url: URL_2,
type: "GET",
async: false,
cache: false
}).done(function (data2) {
myObj2 = process_more(data2, URL_2, someObj);
if (myCounter== 0) {
console.log("%%%% COMPLETE %%%%");

Thank you in advance for your time!!


Answer Source

As it turns out, the code works just fine as long as the jQuery version is greater than 1.8 (which I knew but hadn't noticed that I was using the last version). I replaced jQuery with the latest version and everything is working as expected. However @Bergi is right about async:false being useless or even cause problems.

In earlier versions of jQuery the promise/deferred model is "broken" and is not working as expected/should w.r.t. the original promise model ( ).

See also:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download