0

I am trying to get an array of "records" that each contain an array of "field objects" (each with properties .name, .value, and .fid).

Each array of records is obtained via an AJAX call to a database via an API call.

I want to be able to iterate through these later with some other code, but, for the moment, I'm trying to get the resultant array of arrays.

I'll be honest, I'm struggling with how to do this with promises...

If I was doing this synchronously I'd use a FOR TO loop and push each iteration into an array, but async is causing me issues...

My current code is as follows. This returns a single array of fields, rather tham an array of arrays. I think I need to push each 'thisArray' to another array. Anyone willing to help?:

NOTE:

ridList is a simple array of DB record IDs, e.g.: ["391", "392", "393", "394", "395", "398", "399", "400", "396", "397", "437"]

AVMI_db is the URL the AJAX call gets its data from.

///////////////////////////////////////////////////////////////
// Get record info for each array member
///////////////////////////////////////////////////////////////
function AVMI_getMultipleRecordInfoFromArray(ridList, AVMI_db) {
    var promises = [];

    $.each(ridList, function (index,value) {
        var def = new $.Deferred();
        var thisArray = [];
        $.get(AVMI_db, {   //******* ITERATIVE AJAX CALL ******* 
            act: 'API_GetRecordInfo', 
            rid: value
        }).then(function(xml2) {
            $(xml2).find('field').each(function() {
                var $field = {};
                $field.fid = $(this).find('fid').text();
                $field.name = $(this).find('name').text();
                $field.value = $(this).find('value').text();
                thisArray.push($field);
            });
            def.resolve(thisArray);
        });
        promises.push(def);    
    });    

    return $.when.apply(undefined, promises).promise();
};
  • Not the usual duplicate, but you may find your answer here as there's lots of info on promises and deferred: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323 – freedomn-m Feb 21 '17 at 16:22

2 Answers2

0

Maybe I'm missing something here, please feel free to comment if I didn't understood what you want. I undertood that you want to get an array of arrays instead of an array of fields. For this purpose, you gave the answer in your own question:

I think I need to push each 'thisArray' to another array.

But what puzzles me and makes me think I could have misunderstood something, is that if you can do code a bit complex as this, I think you could have made a simple change as that (or do you got this code somewhere ?), but anyway... I can't test this because I would have to create the whole API call, but it should be as simple as:

///////////////////////////////////////////////////////////////
// Get record info for each array member
///////////////////////////////////////////////////////////////
function AVMI_getMultipleRecordInfoFromArray(ridList, AVMI_db) {
    var promises = [];

    $.each(ridList, function (index,value) {
        var def = new $.Deferred();
        var thisArray = [];
        $.get(AVMI_db, {   //******* ITERATIVE AJAX CALL ******* 
            act: 'API_GetRecordInfo', 
            rid: value
        }).then(function(xml2) {
            $(xml2).find('field').each(function() {
                var $field = [];
                $field.push($(this).find('fid').text());
                $field.push($(this).find('name').text());
                $field.push($(this).find('value').text());
                thisArray.push($fields);
            });
            def.resolve(thisArray);
        });
        promises.push(def);    
    });    

    return $.when.apply(undefined, promises).promise();
Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
0

You can create an array which is populated from as you iterate over the calls and return the array out after the promises are executed.

function AVMI_getMultipleRecordInfoFromArray(ridList, AVMI_db) {
var promises = [];
var returnValue = []; // to be returned by the promise
$.each(ridList, function (index,value) {
    var def = new $.Deferred();
    var thisArray = [];
    $.get(AVMI_db, {   //******* ITERATIVE AJAX CALL ******* 
        act: 'API_GetRecordInfo', 
        rid: value
    }).then(function(xml2) {
        $(xml2).find('field').each(function() {
            var $field = {};
            $field.fid = $(this).find('fid').text();
            $field.name = $(this).find('name').text();
            $field.value = $(this).find('value').text();
            thisArray.push($field);
        });
        returnValue.push(thisArray);
        def.resolve(thisArray);
    });
    promises.push(def);    
});    

return $.when.apply(undefined, promises).then(function() { 
  return returnValue; 
}).promise();
};
Ashwin
  • 115
  • 1
  • 6