0

i have one observable function named processList(request) which returns list of processes inside the page, and i need to get list of linked processes for each returned process, for that i use another observable function which returns list of linked processes for given process linkedProcessesList(process.id), so like this:

    let listOfProcesses = [];
    let listOfLinkedProcesses = [];

    this.processEndpoint.processList(request).subscribe(result => {    
        //getting list of processes for that page
        listOfProcesses = result.data;

        // now i need to get list of linked processes for each process in the listOfProcesses array
        listOfProcesses.forEach(process => {
            this.processEndpoint.linkedProcessesList((process.id).subscribe(result1 => {                
                    // here how do i populate listOfLinkedProcesses array?



                    // this.processToProcessLinkList.push(...result1.data); <- this doesn't work gives an error
                }
            });
        }
    }

Sakén
  • 197
  • 1
  • 12

2 Answers2

0

You could use switchMap along with forkJoin. Try the following

this.processEndpoint.processList(request)
  .pipe(
    map(result => {
      this.listOfProcesses = result.data;
      return linkedProcesses = result.data.map(item => this.processEndpoint.linkedProcessesList(item.id));
    }),
    switchMap(linkedProcessesObservables => forkJoin(linkedProcessesObservables))
  )
  .subscribe(linkedProcessesResult => {
    this.listOfLinkedProcesses = linkedProcessesResult;
  });

I didn't test the code, so there might be some kinks to iron out.

ruth
  • 29,535
  • 4
  • 30
  • 57
  • Could you please say which line do you get the error? – ruth Apr 02 '20 at 22:15
  • Error: http failure response for http://localhost/api/workspace/process/list... 500 Internal Server Error – Sakén Apr 03 '20 at 07:02
  • i think this article is more related to the question https://stackoverflow.com/questions/44593900/rxjs-one-observable-feeding-into-another – Sakén Apr 03 '20 at 07:03
  • @Saken: We are doing the same thing except with `switchMap`. You could also use `flatMap` in this scenario, shouldn't make any difference. However you need to use `forkJoin` because you need to subscribe to multiple observables. In the attached question, they only subscribe to one observable so they didn't need `forkJoin`. – ruth Apr 03 '20 at 07:29
  • @Saken: 500 Internal Server Error is unrelated to this error. It is thrown when there is some kind of backend-side server error when you are making a HTTP request. Most probably the HTTP call isn't formulated properly, or the backend isn't working as expected. To solve that you need to show the implementation of `this.processEndpoint.processList()` and `this.processEndpoint.linkedProcessesList()` functions. – ruth Apr 03 '20 at 07:31
0

Using forkJoin, you can request multiple requests. It is like Promise.all but for observable.

let character = this.http.get("https://api.co/api/people/1");
let characterHomeworld = this.http.get("http://api.co/api/planets/1");
forkJoin([character, characterHomeworld]).subscribe(results => {
  results[0].homeworld = results[1];
  this.loadedCharacter = results[0];
});
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
  • yes, but here first i need to get list of items(processes) from first observable then send the results as input to the second observable can i do it with just the forkJoin method? i guess i need some more methods to achieve that, please correct me if i'm wrong – Sakén Apr 02 '20 at 19:19
  • inside observable for each result i need to get id (result1.id) and for each id get result from another observable and assign the result to the result of the first one (result1.res = anotherObservable.result), i think you get what i need. How to use forkJoin in that case? Thanks – Sakén Jan 11 '21 at 07:53