0

I would like to read parameters from the URL and with that value then call an observable. If a result has been found I'd like to continue with it and call another observable. Ideally both calls are wrapped and I only have to subscribe once and I get both results back together.

This is my implementation so far:

this.route.paramMap.pipe(
  switchMap(params => {
    return forkJoin(
      this.treeService.getTree(params.get('treeName')),
      this.treeService.getTree(params.get('treeName')).pipe(
        mergeMap(tree => this.fruitService.getFruit(tree.id))
      )
    )
  })
).subscribe(res => console.log(res));

this works just fine, but I have to make 2 of the same calls .. how can I improve it so it only does one call to the treeService?

1 Answer 1

3

You can use an inner map like this:

this.route.paramMap.pipe(
  switchMap(params => {
    return this.treeService.getTree(params.get('treeName')).pipe(
      switchMap(tree => this.fruitService.getFruit(tree.id).pipe(
        map(fruit => ([tree, fruit]))
      ))
    )
  })
).subscribe(res => console.log(res));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.