1

I have the following function that returns a jQuery deferred object:

function performAjaxRequest(arg1, arg2, arg3) {
    return $.ajax({
        url: someurl,
        type: "POST",
        data: { arg1: arg1, arg2: arg2, arg3: arg3 }
        success: function(data) {
            //do something
        },
        error: function(data) {
            //do something else
        }
   });
}

Next, I have the following function which does some processing, then calls the function above inside a loop. Finally, it pushes the deferred objects returned by performAjaxRequest() into an array and returns the array:

function putDeferredsInArray() {
    //do some processing
    var deferreds = [];
    var arg1, arg2, arg3 = []; //these arrays are being populated, but that is not important
    var someCount = $('#someFieldThatHasCount').val();
    for (var i = 0; i < someCount; i++) {
        //put the deferred objects in array
        deferreds.push(performAjaxRequest(arg1[i], arg2[i], arg3[i]));
    }
    return deferreds;
}

Finally, I have one last function that completes processing by calling .done for the deferred objects returned from the above function:

function completeProcessing() {
    putDeferredsInArray().done(someCallback(arg)); //getting the error here
}

Have I left something out? For some reason, someCallback(arg) is called even though I'm getting an error that says:

Object doesn't support property or method 'done'

My understanding of jQuery deferred objects may be incomplete, so please correct me if my implementation is wrong. I just need the callback function to run after every AJAX request is complete.

norbitrial
  • 14,716
  • 7
  • 32
  • 59
ic3man7019
  • 721
  • 6
  • 24
  • @HereticMonkey - I'm such a numpy. Thanks. :-) – T.J. Crowder Dec 03 '19 at 16:35
  • 1
    @T.J.Crowder, thank you for your reply. I am working on getting this fixed. I must say, while I do understand the rules of this site concerning duplicate questions, I would've preferred to mark your original post as the answer, as it addressed the specific error I had with trying to treat an array of deferred objects as a deferred object. Anyway, thanks again. Your explanation was tremendously helpful. – ic3man7019 Dec 03 '19 at 16:39
  • Glad that helped! :-) Yeah, I often leave a comment explaining how the duplicate applies to "this" question, but in this case was in the middle of something. It sounds like you had access the that answer and got what you need, but just in case: https://pastebin.com/4EmDFCqt – T.J. Crowder Dec 03 '19 at 16:49
  • 1
    @T.J.Crowder thanks! – ic3man7019 Dec 03 '19 at 16:57

0 Answers0