0

I'm sorry, you guys. I really hate having to ask the question; I promise, I've been through as many other questions that look even tangentially related as my patience will allow.

Following the code from the following questions:

I've got the following:

var XHR = [];

//This parses some selections on a screen, which, in turn, informs the URL inside the loop.
$("#list > input:checkbox:checked").each(function(){
    result = [];
    var list = $(this).val();
    XHR.push($.ajax({
        type:'GET',
        url:"https://a-place.com/subsite/webapp/_api/web/lists/getByTitle('"+list+"')/items?$select=" + select + "&$filter=" + filter + "&$expand=" + expand + "&$top=10000",
        dataType: "json",
        headers: {Accept:"application/json;odata=verbose"},
        complete:function(data){}
    }));
});

$.when(XHR).then(function(data){
    console.log(data);
});

No matter what I do, I'm only ever getting the promises inside that .when.then construction. I can't do anything with them, trying to access the responseJSON property, where all the actual objects are, does nothing. I've tried adding a return statement inside the complete callback function, that doesn't appear to change what's actually getting pushed into the XHR array.

Ideally, this is supposed to return a bunch of list items from one or more SharePoint lists that match the selected options and put the matched items into a single array that I can do stuff with.

Edit

Hmm. Ok, based on advice, I've tried:

success:function(data){}

And

success:function(data){return data.d.results;}

And nothing really changes in the console regardless of whether I use $.when.then or .done.

Thanks in advance.

Community
  • 1
  • 1
ChrisPEditor
  • 215
  • 2
  • 12
  • Think maybe you want `.done(...)` instead if `.then(...)` – John Hascall Jan 05 '16 at 20:12
  • I appreciate the advice, but I've already tried that. Same exact result. – ChrisPEditor Jan 05 '16 at 20:16
  • Instead of returning result in success callback, try to push it into array. `result.push(data.d.results);` And afterwards in "then" function you will know that you have all results ready, and you can operate with it. Not the fancy solution here, but should work. – Borys Kupar Jan 05 '16 at 20:42
  • Awesome, @Borys, that worked. I can work with this. You should write this in as the answer so I can mark it. Thanks! – ChrisPEditor Jan 05 '16 at 21:46

3 Answers3

1

You can try collecting all results in your success callback, and use it afterwards in 'then' function callback:

var results = [];
...
success: function(data){
    results.push(data.d.results);
}
...
$.when(XHR).then(function(){
   // Here you should have all results filled in
   console.log(results);
});
Borys Kupar
  • 1,631
  • 11
  • 24
0

Your usage of $.when is incorrect. $.when is not implemented like Promise.all, instead it expects multiple parameters. Fortunately, this is very easy to get around.

$.when.apply($, XHR).then(...
Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • Thanks for the advice. I've tried chaining .apply to the $.when and all that I ever get is a weird array that includes, in this order, an object containing data from only the first ajax call, the string "success", and then another promise with data I can't get at. – ChrisPEditor Jan 05 '16 at 21:43
  • `data` should be an array of arrays or objects. – Kevin B Jan 05 '16 at 21:44
  • Like I said, 'data' ends up as an array containing an object with only _one_ of the ajax calls' data, the string "success", and a promise object. – ChrisPEditor Jan 05 '16 at 21:47
  • that suggests `XHR` only has one promise, in which case you would get an array containing the response, textStatus, and the jqXHR. – Kevin B Jan 05 '16 at 21:48
-1

Instead of using the complete callback you need to set the success callback. The docs says that the success callback has a data argument and also that the complete callback executes after the success and error callbacks.

A sample I have from my code: (the ApplyUpdate function gets called with the json data that is then applied to my existing objects)

    function doRefresh(repeat) {
    if (nextTimeOut) clearTimeout(nextTimeOut);
    j$.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: baseUrl + "/WebService/Service.asmx/GetSummary",
        data: "{}",
        dataType: "json",
        success: function (json) { ApplyUpdate(json.d, repeat); }
    });
}

function ApplyUpdate(resp, repeat) {
    goodObj.text(resp.GoodCount);
    errObj.text(resp.BadCount);
    if (repeat) nextTimeOut = setTimeout(autoRefresh, waitTime);
}
J. Schmale
  • 446
  • 3
  • 6