Using callbacks, I have the following code
var TK = {
List: [],
getSectionA: function(listName, callback)
{
var arrayList = [];
$.get('ajax/test.html', {sendName: listName}, function(data) {
$.each(data, function()
{
arrayList = data;
});
callback(arrayList);
});
}
};
calling the function
$("#dropDownList").change(function()
{
TK.getSectionA(fileName, function(data)
{
TK.List = data;
alert(TK.List); // This works ok...
});
alert(TK.List); // This does not work when calling using this property outside the callback..
});
How can I get this to work outside the callback function which is in another function
the jQuery get() function returns the promise interface that allows you to add as many functions as you want to handle the get() completing either successfully (done()) or failing (fail) or just completing either way (always()).
The promise interface is the same as the deferred but only has the “read only” methods so you can’t interfere with the process but you can see what happens.
If you return the promise, it can be used to do whatever you want:
Call it something like this:
You can read it on this page http://api.jquery.com/jQuery.ajax/ but have to skip down to a paragraph that has “promise” in it.