0
votes

I have to call three web services from an Angular2 app, and I have to chain them. Both of them can be called together, so I use forkJoin. But later, I have to use an Id returned from one of the service to call another service. Which is the proper way of chain the last service with the result of the forkJoin?

Althought I can use flatMap, this last service does not make a transformation of what it is got from the forkJoin (map and flatMap are called transforming operators. They should only be used when we’re transforming the returned data to something else).

Should I subscribe to the forkJoin and then, in case of the success, subscribe again to the last service (I don't like it)? Or should I use Zip (or ZipAll) operator? Thank you very much in advance.

Edit: I'm gonna provide an example. Let says I have to get some project information, and I have three services:

  1. getProject(projectId): get information about a project, like its title, duration, ... and an projectPropertiesId with some properties then the project can have.

  2. getProperties(): get the list of all properties than any project can have.

  3. getProjectProperties(propertiesId): get a list of the selected properties. One solution could be:

this.subscriber1 = Observable.forkJoin(this.projectService.getProject(projectId), this.projectService.getProperties())
.subscribe(data => {
  this.project = data[0];
  this.properties = data[1];
  this.subscriber2 = 
  this.projectService.getProperties(this.project.propertiesId)
    .subscribe(selectedProperties => {
       // save the selected properties...
    }
});

Shouldn't be better to after you have the forkJoin, use the Zip operator to chain the last service call (projectService.getProperties) instead to have a subscriber inside another subscriber?

1
Welcome to Stack Overflow, unfortunately, for the moment, the answer is : it depends. Maybe you could add some code to show us what you have done. It would then be easier to figure out what you are trying to achieve. - n00dl3
"I have to call three web services" vs. "Both of them" 3 or 2? "I have to chain them" vs. "I use forkJoin" Chain or fork? An example would be useful. - a better oliver
I edited the question with an example... - Pablo Anglat

1 Answers

0
votes

It is generally not recommended to nest subscriptions and you should keep everything in a single stream. I don't fully get your example but I think what you are asking is to call 2 services in parallel, once they both return, then call a third service with values from the resulting 2 first service calls.

You could do it this way, assuming that both of the initial service's observables complete. forkJoin waits for both to complete.

this.subscription = Observable
    .forkJoin([this.projectService.getProject(projectId), this.projectService.getProperties()])
    .switchMap(([project, properties]) => this.projectService.getProjectProperties(project.somevalue, properties.somevalue))
    .subscribe(data => {
        // save the selected properties...
    });