0

I am trying to add/remove data to the projectInfo variable on the success callback. The values don't seem to be sticking.

function getIssues(projectInfo, callback) {
var projectData = projectInfo;

var promises = [];

for (keys = 0; keys < projectData.length; keys++) {

        var urlListData = // A url 
        var result = $.ajax({
            url: urlListData,
            type: "GET",
            async: true,
            beforeSend: function (xhr) {
                xhr.setRequestHeader("ACCEPT", accept);
            },
            success: function (xhr, textStatus) {
                if (xhr.d.results == "") {
                    console.log("I am removing this value" + xhr.d.results);
                    projectData.splice(keys, 1);
                    keys--;
                }
                else {
                    console.log("I add Issues");
                    console.log("The value of projectData is " + projectData[keys].Issues);
                    projectData[keys].Issues = xhr.d.results;
                    console.log("The value of projectData after setting is " + projectData[keys].Issues);
                }
            },
            error: function(xhr, ajaxOptions, thrownError){
                projectData.splice(keys, 1);
                keys--;
            }      
        })

        promises.push(result);
}

$.when.apply(null, promises).done(function () {
    console.log("Calling in the when promise");
    callback(projectData);
});

}

When I call the getIssues method, I check the console.log values. The log values actually show that Issues are being added and removed then the when promise occurs. However, the value returned from getIssue method doesn't contain any of the added or removed values. What am I doing wrong/missing?

Jaspreet Chauhan
  • 191
  • 4
  • 14
  • `getIssues` doesn't `return` anything? Do you mean the callback argument? What are you passing in as `projectInfo`? – Bergi Jan 20 '15 at 04:39
  • Yes the callback argument. I am passing in a JavaScript object. – Jaspreet Chauhan Jan 20 '15 at 04:46
  • To clarify a bit further. In the when promise, if I were to set console.log(projectData) it would show an empty object and did not have values set/removed from the success callback – Jaspreet Chauhan Jan 20 '15 at 05:00
  • I'd guess that you're running into a [scope problem](http://stackoverflow.com/q/750486/1048572) with you `key` variable; Try to log it in the callbacks – Bergi Jan 20 '15 at 15:25

1 Answers1

0

I simplified a bit the problem and that works for me:

    function getIssues(urls, array, callback) {
        var promises = [];
        for (var key = 0; key < urls.length; key++) {

                var result = $.ajax({
                    url: urls[key],
                    type: "GET",
                    async: true,
                    success: function(xhr, textStatus) {
                        array.splice(-1)
                    }
                })

            promises.push(result);
        }

        $.when.apply(null, promises).done(function() {
            console.log("Calling in the when promise");
            callback(array);
        });
    }

    getIssues(["/firstUrl", "/secondUrl"], [1,2,3], function(array) {
        console.log('done', array); // array is [1] here ('spliced' twice)
    });

so running this (remember about changing /firstUrl and /secondUrl), you can isolate your problem.

andrusieczko
  • 2,824
  • 12
  • 23