I have written some code that utilizes two pipes to load a product instance along with its playlist. In case there is an error while loading the playlist, the property is set to null:
getProduct(productId: number): Observable<ProductDTO> {
return this.internalFindOne(productId).pipe(
flatMap((productDTO: ProductDTO) => {
return this.productPlaylistService.getProductPlaylist(productId).pipe(
map((productPlaylistDTO: ProductPlaylistDTO) => {
productDTO.playlist = productPlaylistDTO;
return productDTO;
}),
catchError(() => {
// If product playlist doesn't find anything, we still have to return the product dto object
productDTO.playlist = null;
return of(productDTO);
}),
);
})
);
}
As it currently stands, everything works fine. However, I am unsure about using flatMap
in this context. While there are no compiler errors, based on my understanding, flatMap
operates in a 1:n manner, whereas map
does so in a 1:1 fashion. The code expects a productDTO
as input and returns the modified productDTO
. When I replace the flatMap
with just map
, the nested map
function isn't called (neither the success nor error parts).
The classes ProductDTO
and ProductPlaylistDTO
are not iterable.
Could someone help me understand what I might be missing here?