I am dealing with this code. I need top add another ajax to get list items base on first loop. The result is not correct unless write alert("WAIT WAIT").
loadRestRequest("/_api/Web/lists/GetByTitle('KSF')/items?$filter=Master eq 94 " ,
function (data) {
$.each(data.d.results, function (idx, val) {
ksfId = val.ID;
//do some stuff
loadRestRequest("/_api/Web/lists/GetByTitle('KPI')/items?$filter=KSF eq " + ksfId,
function (data) {
//do some stuff
});
alert("WAIT WAIT");
});
});
I call below function twice to get list items
function loadRestRequest(url, callback) {
var async = true;
var res = null;
if(callback == null)
{
async = false;
callback = function (data) {
res = data.d;
}
}
$.ajax
url: url,
typ({
e: "GET",
async: async,
headers: {
"accept": "application/json;odata=verbose",
},
success: callback,
error: function (error) {
alert(JSON.stringify(error));
}
});
return res;
}
I googled this problem and I found out these functions are not synchronized. Calling wait or sleep function is not working right because there is no exact estimation for query time. I should work with $.Deferred but I don`t know how to use it in my loadRestRequest function. my code is running on sharepoint 2013