Whenever I attempt to use Promise.then(), the events from this.events, this.tmEvents, and this.totalEvents keep logging before the promises are fully complete. Even when I tried implementing async/await to prevent this, I faced the same issue. Is there a way to delay these functions until the promises are resolved? Interestingly, if I log this.tmEvents in the getEvents function, it always shows up after those 3 events.
//this.data.tmevents (Dataservice)
public tmEventsSource = new BehaviorSubject<any>([]);
tmEvents = this.tmEventsSource.asObservable();
newTmevents(tmEvents) {
this.tmEventsSource.next(tmEvents);
}
async fetchEvents() {
await this.getEvents();
await this.getTmEvents();
this.data.tmEvents.subscribe(tmEvents => {
this.totalEvents = this.totalEvents.concat(this.events);
this.totalEvents = this.totalEvents.concat(tmEvents);
console.log(this.events);
console.log(tmEvents);
console.log(this.totalEvents);
this.shuffle(this.totalEvents);
});
async getTmEvents() {
const storedTMEvents = JSON.parse(localStorage.getItem(this.TM_EVENTS));
if (storedTMEvents == null) {
try {
const res = await this.getUserLocationServ();
await this.getTmEventsServ(res);
} catch(err) {
console.log(err);
}
} else {
this.data.newTmevents(storedTMEvents);
}
}