I am trying to add some values to a set and its not working as intended. when i console.log, it shows set size is zero however when i expand it, it shows values aswell as size. I am assuming this is happening because of asynchronicity but i am not sure how to fix it. I have attached the code below. I have reviewed some of the other threads related to async code but i am still having trouble understanding it. I suppose i have to use promise and await? but i am not sure how and where. Any help would be appreciated. Thank you.
addWeeks() {
let result: any;
let weeksSet = new Set();
forkJoin({
weeksList: this.weeksService.getWeeksList(),
}).subscribe((vals) => {
const { weeksList } = vals;
result = weeksList.body;
if (result != null) {
this.gridData = result["weeksList"];
this.gridData.forEach((item) => {
weeksSet.add(item);
});
}
});
}
Screenshot of what i see when i console.log weeksSet.