Let us say I am making multiple API calls from a React application. "Multiple API Calls" in this context means: Clicking on a checkbox several time fast. Everytime a checkbox is checked, an API call is triggered.
To illustrate this situation more precisely, I will add a screenshot off the User Interface:
Everytime a checkbox is checked, this function is called:
export function getApplications(authenticationToken, url, query, queryStringIn) {
return function(dispatch) {
const config = {
headers: {'Authorization' :"Bearer " + authenticationToken}
};
let queryString;
if (queryStringIn === "" || queryStringIn === null) {
queryString = queryStringBuilder.buildQuery(query);
} else {
queryString = queryStringIn;
}
let fetchTask = fetch(url + "/Applications?" + queryString.toString(), config).then(data => {
return data.json();
}).then(applications => {
dispatch(loadApplicationsSuccess(applications, queryString, query));
}).catch(error => {
dispatch(loadApplicationsFailed(error));
});
addTask(fetchTask); //ensure that server rendering is waiting for this to complete
return fetchTask;
}
The problem occurs when I am clicking at a checkbox so fast that the API is not able to return a response, before I am making a new call to the same API.
I want a behavior where only the last API call is made, and API calls in the queue before are cancelled if there are any.
What do you guys recommend to do in a such case?