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:
getProject(projectId): get information about a project, like its title, duration, ... and an projectPropertiesId with some properties then the project can have.getProperties(): get the list of all properties than any project can have.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?