I am using Promise to execute my AJAX function, and am using `.then()' when calling the function to imply that anything in the .then() scope is executed once the AJAX function is executed. However, the .then() still executes before the AJAX function is fully done retrieving data from the server. I have been stuck on this for really long, and am unable to figure out what exactly is causing the issue. Is my code wrong? Below is my code. Any help will be deeply appreciated!
Calling the AJAX Function
userLayer = this.addUserLayerTreemail(
$loadingContent,
$loadingDiv,
treeLayers,
map,
heritage_cluster_layer_view_18,
flowering_cluster_layer_view_18,
IMP_FIELDS,
suggestion,
userPlottedLayerView18Options,
userLayer,
$speciesError
).then( function(response){
//this gets executed before the function retrieves all the data from the server - it should return true, but returns false
console.log(map.hasLayer(userLayer), "check if layer exists")
setSearchState(true);
$mapSearch.blur();
}).catch(function(err){
console.log("error caught", err)
});
Defining the AJAX Function
addUserLayerTreemail(
$loadingContent,
$loadingDiv,
treeLayers,
map,
heritage_cluster_layer_view_18,
flowering_cluster_layer_view_18,
IMP_FIELDS,
suggestion,
userPlottedLayerView18Options,
userLayer,
$speciesError
) {
// let checked = this.getCurrentFilter(),
const featureType = 'USERTREE';
let queryString = '';
suggestion = suggestion.toLowerCase();
let $selectedOptionSearch = $('.mapFilterBtns.is-active').data('value');
let all_trees="";
let searchText= $('#mapSearch').val();
let configData = {"searchText": searchText, "searchType" : $selectedOptionSearch};
console.log("config", configData)
// this function is called in the resolve() function below
function retrieveTrees(response) {
let conditionalVal = " or ";
let treeID = "";
for(let i=0; i<response.length; i++) {
treeID = response[i]['MYTreeID']
if(i==0) {
all_trees = `${IMP_FIELDS.TREEMAIL_SEARCH} like '%${treeID}%'`;
} else {
all_trees += conditionalVal + `${IMP_FIELDS.TREEMAIL_SEARCH} like '%${treeID}%'`;
}
}
console.log("all trees here",all_trees);
queryString = all_trees + ` and Actual_Tree_Type like '%${featureType}%'`;
let isSearch = true;
let layerOptions = $.extend(
{
where: queryString
},
userPlottedLayerView18Options
);
layerOptions.url = mapLayers.userPlantedLayer;
userLayer = L.esri.featureLayer(layerOptions);
userLayer
.query()
.where(queryString)
.run((err, featureCollection, response) => {
if (featureCollection.features.length > 0) {
removeClass($loadingDiv, 'show');
//adding the userLayer to the map
userLayer.addTo(map);
//this returns true after the .then() chunk is executed in the call above
console.log(map.hasLayer(userLayer), "checking in ajax")
});
console.log(map.hasLayer(userLayer), "checking in ajax ")
return userLayer;
}
return new Promise(function(resolve, reject){
$.ajax({
url: "../apis/treemailSearch",
method: 'GET',
dataType: 'json',
data : configData,
contentType: 'application/json',
success: response => {
return resolve(retrieveTrees(response));
},
error : function(err){
reject(err, "printing error here");
}
});
})
}