In this function, I am trying to run 2 different ajax requests (3 options, only 2 will run depending on if statements). addDep()
, and addCust()
functions both have an ajax request in them and get sent an url. However, I need that ajax function to run and complete before the code in the .then function being called after it runs. Right now the code runs but has null values because it hasn't got data1 or data2 back from the ajax function. How do I fix this?
function builder(){
var beforeDate = dateConv(document.getElementById('Before').value);
var afterDate = dateConv(document.getElementById('After').value);
var url = "http://localhost:8181/GRAIN/grain_map?start="
url += afterDate;
url += "&end=";
url += beforeDate;
url += "&attributes=DATE-SHIPPED+DESTINATION-NAME+DESTINATION";
ajaxReq(url).then(function (jsonString) {
for (i=0; i < jsonString.grain_map.length; i++){
destinationArray.push(jsonString.grain_map[i].DESTINATION);
}
for (i = 0; i < destinationArray.length; i++){
if (destinationArray[i] == ""){
continue;
}
if (destinationArray[i].length == 3 || destinationArray[i].length == 2){
var destCode = destinationArray[i];
addDep(destCode).then(function (data1) {
var address = "";
var depArray = data1.dep_address[0].ADDRESS;
for(i = 0; i < depArray.length; i++){
address += data1.dep_address[0].ADDRESS[i] + " ";
}
addressArray.push(address);
}).catch(function (err) {
console.error(err);
});
}else if (destinationArray[i].length == 6){
var destCode = destinationArray[i];
addCust(destCode).then(function (data2) {
var address = "";
var depArray = data2.customer_address[0].ADDRESS;
for(i = 0; i < depArray.length; i++){
address += data2.customer_address[0].ADDRESS[i] + " ";
}
addressArray.push(address);
}).catch(function (err) {
console.error(err);
});
}
}
}).catch(function (err) {
console.error(err);
});
}