I have tried this several ways, using .done and .then, and I get the same result - the data seems to populate the array just fine ( I can console.log the array inside of "pushTeamMembers" ), but the array is empty when I try to view it in the main flow.
I thought it might be some sort of a scope issue, but it works great if I put a slight pause ( set interval ) before the last console.log - which seems to imply that the program is displaying the data before the data is actually pulled down. Thanks.
var siteurl = _spPageContextInfo.webAbsoluteUrl;
var teamMembers = [];
// ********** Ajax query
var teamListData = $.ajax({
type: 'get',
url: siteurl + "/_api/web/lists/getbytitle('The Team')/items",
headers: { "Accept": "application/json; odata=verbose" }
});
// ********** Ajax query deferred ".then" handler
teamListData.then(function (value) {
$.each(teamListData, function(i, item) {
var thisNameId = teamListData[i].NameId;
var workStream = teamListData[i].Title;
var member = {id: thisNameId, workstream: workStream};
pushTeamMembers(member);
});
});
// ********** Push onto array
function pushTeamMembers(member)
{
teamMembers.push(member);
// ********** This shows that the array is populated.
console.log(teamMembers);
}
// ********** This shows that the array is empty - unless I put a
// slight pause here ( set interval ).
console.log(teamMembers);