I call the following function:
function uploadReturns() {
var uploads = me.getIssuesFromReturnsList();
uploadreturnsobjects(uploads); //process the uploads object and send it to an AJAX call
}
In me.getIssuesFromReturnsList
the uploadList
variable is being returned before the rest of the code executes because it is asynchronous (as per this question)
me.getIssuesFromReturnsList = function () {
var uploadList = [];
$.each(me.returnsList(), function (index, i) {
var issue = issueRepository.GetDefaultIssue();
issue.barcode = i.barcode;
issue.ReturnsAmount(i.amount);
var uploadItem = {
barcode: i.barcode,
amount: i.amount,
issue: ko.observable(issue)
};
uploadList.push(uploadItem);
issueRepository.GetIssuesOffline(i.barcode, function (issues) {
if (issues.length > 0) {
uploadItem.issue(issues[0]);
}
});
});
return uploadList;
}
How can I alter this code so that the calls are no longer asynchronous, and instead waits for all the inner code to execute before returning the uploadList
?