Here is a method I am working with:
prepareCalendarDaysToAdd() {
this.getDaysToAdd(); //first line
this.dataSaved.emit(this.daysToAdd); //second line
}
The getDaysToAdd() method makes an asynchronous call:
getDaysToAdd() {
this.daysService.getDaysInSpecificYearWith(this.toDate.year).subscribe(
data => {
this.getCalendarDaysFrom(data);
},
error => { console.error(error); },
() => {
this.getCalendarDaysBetween();
}
)
}
The issue here is that the data emits before it's fully prepared. How can I ensure that the async call from the first line is completed before proceeding?
Therefore, the desired code looks like:
prepareCalendarDaysToAdd() {
check if getDaysToAdd has finished then emit the list of days
}
I attempted to use promises to handle this situation, but I seem to be misunderstanding how they should work.
prepareCalendarDaysToAdd() {
const promise = new Promise((resolve, reject) => {
this.getDaysToAdd(); //first line
resolve();
})
.then(
() => {
this.dataSaved.emit(this.daysToAdd); //second line
}
);
});