Надеюсь, я правильно понял вопрос.
Итак, вашей первой проблемой было то, что первый вызов API возвращает массив. Эту проблему можно решить с помощью mergeMap
- сглаженного массива, так что наблюдаемая нисходящая линия будет испускать 3 сервиса последовательно.
getNewestNursingServices() {
return this.http.get('api/nursing-service/newest')
.pipe(
mergeMap((services: []) => {
// `of` will return an observable which emits the items of the array after each other
return of(services);
}),
mergeMap(service => this.getImage('PREVIEW', service.uuid)),
tap((image: Image) => {
// here you can do sideeffects with the images, eg. pushing them into an array somewhere...
}),
reduce(
(allImages: Image[], currentImage: Image) => {
// ... or you can collect them into an array, so the downstream will be `Observable<Image[]>`
allImages.push(currentImage);
return allImages;
},
[],
),
);
}
Что касается того, что вы должны подписаться, это не так, вы можете использовать результирующая наблюдаемая, например, в трубе async
, если вы не хотите использовать шаблон побочного эффекта + подписки.