'Wait until while loop with request is finished
I have a date rangepicker where I select two dates. Then I check via an API for each day in a while loop if it is a holiday. Unfortunately I can't get the right result. I only get 0. Where is the mistake?
function(start, end, label) {
var from = new Date(start.format('YYYY-MM-DD'));
var to = new Date(end.format('YYYY-MM-DD'));
var loop = new Date(from);
let days = 0;
while (loop <= to) {
let day = ("0" + loop.getDate()).slice(-2);
let month = ("0" + (loop.getMonth() + 1)).slice(-2);
let myday = (day) + "." + (month) + "." + loop.getFullYear();
$.get("https://ipty.de/feiertag/api.php", {
'do': "isFeiertag",
'datum': myday,
'loc': "BW"
}).done(function(data) {
if ('0' == data) {
days++;
}
});
var newDate = loop.setDate(loop.getDate() + 1);
loop = new Date(newDate);
}
console.log(days);
}
Solution 1:[1]
I would probably advise you to execute the ajax calls inside Promises, and then use Promise.all to manage the outcomes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Telmo Dias |
