Ok I got it working...
and it was not quite what sambomartin was saying...
Basically I moved the $.when inside of goAjax() and had it watching the jqxhr object. I still call goajax from within go ajax, but it is now waiting to make that call.
Here is the code:
function goAJAX(url) {
var jqxhr = $.ajax({
dataType: "xml",
url: url
}).fail(function (data) {
console.log("error");
console.log(data);
});
$.when(jqxhr).then(function (result) {
var oaiJSON = $.xml2json(result);
var thisBatchOfRecords = oaiJSON.ListRecords.record.length;
var resumptionToken = oaiJSON.ListRecords.resumptionToken.text;
totalRecords += thisBatchOfRecords;
if (thisBatchOfRecords >= 99) {
console.log(totalRecords);
var url = "https://www.someURL.com/oai/request?verb=ListRecords&resumptionToken=" + resumptionToken;
res.html("working...");
goAJAX(url);
} else {
console.log("finsished");
res.html(totalRecords);
}
});
}