Ranjith Varatharajan
Ranjith Varatharajan

Reputation: 1694

combining three observables two using forkjoin and one using mergemap

Three api calls, in which two of them has no parameter to pass. So I used forkJoin the two api calls, but the third one needs a parameter which is gotten from the first two apis. I have no idea how to implement it.

Here is my code so far.

ngOnInit();
{
  const categories$ = this.blogService.getCategories(); // one
  const posts$ = this.blogService.getPosts(); // two

  forkJoin([
    categories$,
    posts$
  ]).pipe(
    map(([categories, posts]) => {
      return posts.map(post => {
        ... // doing some stuff here
        return post;
      });
    }),
    mergeMap(posts => {
      return posts.map(post => this.blogService.getFeaturedImage(post.featured_media)); // three
    }, y => console.log(y))
  ).subscribe();
}

In the above code, forkJoin works as I expected. but the third call is not happening. I need to pass a value to the third service. How to do that?

P.S y returns the value of second call as expected

Upvotes: 0

Views: 517

Answers (1)

Fan Cheung
Fan Cheung

Reputation: 11380

Please try the below code, you were returning an array of observable which needs forkJoin (async) or concat (sequential) to execute them

 mergeMap(posts => {
      return forkJoin(posts.map(post => this.blogService.getFeaturedImage(post.featured_media))); // three
    })

Upvotes: 1

Related Questions