sorry if it's repeated I tried to solve the problem but I couldn't figure how to do
I'm trying to build a website for streaming (Movies, series, ..etc)
I have a function which returns the number of episodes for a show
my ts component code is:
export class MainCarouselComponent implements OnInit {
constructor(
private homeService: HomeService,
private episodeService: EpisodesService
) {}
MainCarousel?: IShow[];
EpisodesNum?: number;
ngOnInit(): void {
this.homeService.getHomeMainCarousel().subscribe((shows) => {
this.MainCarousel = shows;
this.MainCarousel.forEach(
(show) => (show.runtime = this.calculateRuntime(show))
);
});
}
calculateRuntime(_show: IShow): any {
if (_show.runtime > 0) {
var runtime = _show.runtime;
var hours = runtime / 60;
var rhours = Math.floor(hours);
var minutes = (hours - rhours) * 60;
var rminutes = Math.floor(minutes);
return `${rhours} hr ${rminutes} min`;
} else {
return this.calculateEpisodes(_show.id);
}
}
calculateEpisodes(_showId: number): number { //problem is here//
let episodesNum;
this.episodeService
.getEpisodes(_showId)
.subscribe((episodes) => (episodesNum = episodes.length));
return episodesNum;
}
}
I want to return the number of episodes but it returns with undefiend, I know it's async so it doesn't wait, but can someone help me with the correct way to solve it ?
thanks a lot !