I have 2 functions, doFirst() and doSomethingElse(). Now the first function consists of about 10 AJAX Requests which are executed on a condition which checks if that filter was picked. These AJAX Requests are communicating with a URL and save a set of IDs in the respective arrays. After this is done, the doSomethingElse() has to fire which draws the results table. At this point I am executing them by setting a time out. I would like to know a better way to wait for functions to finish and then execute the next function. Any help is greatly appreciated.
Thanks!
//How I am currently calling the functions:
doFirst();
doSomethingElse();
function doFirst(){
var filterArrayTaxSel1 = $("#taxIA span").get().map(el => el.textContent);
for (var i = 0; i < filterArrayTaxSel1.length; i++) {
filterArrayTaxSel1[i] = filterArrayTaxSel1[i].replace(" ", "%20");
}
// taxgroup2 - Selector
queryBuilder = filterArrayTaxSel1;
//console.log(queryBuilder);
console.log(filterArrayTaxSel1);
if (filterArrayTaxSel1.length > 0) {
if (filterArrayTaxSel1.length > 0 && filterArrayTaxSel1[0] != "All") {
console.log("I am inside the IF!");
for (var i = 0; i < filterArrayTaxSel1.length; i++) {
var baseURL = "some URL here";
console.log(baseURL);
responses(baseURL);
function responses(baseURL) {
$.ajax({
url: baseURL,
type: "get",
cache: false,
headers: {
'Content-Type': 'application/json'
},
success: function (data) {
console.log(data.features.length);
for (var i = 0; i < data.features.length; i++) {
if (taxArrayT1.indexOf(data.features[i].properties.taxon_id) == -1) {
taxArrayT1.push(data.features[i].properties.taxon_id);
}
}
console.log("In the Invertebrate Animals Section 1");
console.log(taxArrayT1.length);
}
})
}
}
}
else if (filterArrayTaxSel1[0] == "All") {
console.log("I am inside the IF!");
var baseURL = "some URL here";
console.log(baseURL);
responses(baseURL);
function responses(baseURL) {
$.ajax({
url: baseURL,
type: "get",
cache: false,
headers: {
'Content-Type': 'application/json'
},
success: function (data) {
console.log("I am inside the ELSE IF ALLL!");
console.log(data.features.length);
for (var i = 0; i < data.features.length; i++) {
if (taxArrayT1.indexOf(data.features[i].properties.taxon_id) == -1) {
taxArrayT1.push(data.features[i].properties.taxon_id);
}
}
console.log("In the Invertebrate Animals Section 2");
console.log(taxArrayT1.length);
}
})
}
}
//Selection 1 Tax Group AJAX Call Sensitivity ARRAY WITH 0 to Multiple CASES - ENDS.
}
//some more AJAX Calls depending on whether the filter exists
//End of function
}
function doSomethingElse(){
//Code to draw the Table using the arrays from the previous function
}
3
Answers
Promise
is simply an object to which you can pass some function that does some work, and when it’s finished it invokes a callback function – which you use for further actions.You can find full documentation on: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/prototype
As for example, you have a function, that returns Promise object:
You then make multiple ajax calls:
Then you have a “listener”, that waits for all promises (ajax calls) to be finished:
You have many articles and documentation on that subject:
Here is an example of how you can use
Promise
object to make your async code (such as AJAX requests) a bit easier to handle.Promise.all
seems like it would be a good candidate to help resolve your issue. You could do something like this:From your code you are using jQuery so you can utilize some of its built in methods. You want to basically keep track of all your Ajax calls in an array so when you make an Ajax call, push them into an array. You can then use jQuery’s
$.when()
to execute the next step when they are all complete.Basic idea:
If you were not using jQuery, it would be promises with promise.all