Encountering an issue where manipulating/process response and return triggers an error in TypeScript with the message "Not all code paths return a value.". Data is fetched from a backend API using RxJS lastValueFrom
operator, along with lodash functions as helpers.
For instance, below code snippet causes an error:
async getMoviesByIds(movieIds: string[]) {
try {
const uniqueMovieIds = uniq(movieIds);
const response: any = await lastValueFrom(this.movieService.getMoviesByIds({ movieIds: uniqueMovieIds }));
if (!response?.movies?.length) return [];
const moviesWithNames = uniqueMovieIds.map((mId) => response.movies.find((m: any) => m.id === mId)).filter((movie) => movie?.id);
return moviesWithNames;
} catch (e) {
console.error(e);
}
}
Interestingly, there are no errors when simply returning the response:
async getMoviesByIds(movieIds: string[]) {
try {
const uniqueMovieIds = uniq(movieIds);
const response: any = await lastValueFrom(this.movieService.getMoviesByIds({ movieIds: uniqueMovieIds }));
return response;
} catch (e) {
console.error(e);
}
}
This discrepancy in behavior raises the question of why these two situations have different outcomes.