Panais
Panais

Reputation: 361

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!!

.ajax({
    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);
    myCounter--;
    if (myCounter== 0) {
        console.log("%%%% COMPLETE %%%%");
    }
});

Thank you in advance for your time!!

Pan

Upvotes: 3

Views: 3320

Answers (2)

Fernando León
Fernando León

Reputation: 514

I solved it like this:

for (var i = 0; i < json.length; i++) {

    if(json[i].university_name !== '' && json[i].state_code !== ''){

        $.when(
            $.ajax({
                async: false,
                url: "validateUniversityExist.php",
                method: 'post',
                dataType: 'json',
                data:{
                    'name': json[i].university_name,
                    'state_code' : json[i].state_code
                }
            })).then(function( resp, textStatus, jqXHR ) {

                if(resp.id_university !== '' || resp.id_university !== undefined){

                    if (json[i].record_status == 3){

                        $.ajax({
                            url: "createNewBenefits.php",
                            method: 'post',
                            dataType: 'json',
                            data:{
                                'id_university': resp.id_university,
                                'state_code' : json[i].state_code,
                                'updated' : json[i].updated,
                                'id_visa' : json[i].visa_type,
                                'record_status' : json[i].record_status,
                                'mandatory' : json[i].mandatory,
                                'aca' : json[i].aca
                            }
                        }); // end insert complete record ajax

                    }

                }

            }); // end university when ajax
    }


} // end for

Using when and then. https://api.jquery.com/jquery.when/

Upvotes: 0

Panais
Panais

Reputation: 361

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 ( https://www.promisejs.org/ ).

See also: http://api.jquery.com/deferred.then/

Upvotes: 2

Related Questions