2

In my angular 7 application, I need to chain multiple HTTP Get call and for any call I need to wait for the call before to be completed also if I don't need to use directly the response in the next call.

I don't want to use subscribe inside a subscribe and so on because the code is very bad.

So for example, if I have 4 functions what is the best practice to achieve it?

6
  • you can use async/await Commented Feb 13, 2019 at 13:40
  • If possible I want to avoid promise Commented Feb 13, 2019 at 13:43
  • 1
    You are looking for RxJS switchMap if you want to use the result form the first request in the second request and so on. Use combineLatest or forkJoin if you simply want to await all requests to finish. Commented Feb 13, 2019 at 13:44
  • 1
    you can use forkJoin from 'rxjs'; // RxJS 6 syntax Commented Feb 13, 2019 at 13:44
  • Check if how to call a function after the termination of another function in angular helps Commented Feb 13, 2019 at 13:44

2 Answers 2

4

Try forkJoin:

linkContractToDepartment(data): Observable<any> {
        return this.httpClient.get<any>(
            '/something/link-contract',
            data
        );
}

calls = [];
calls.push(this.linkContractToDepartment(1));
calls.push(this.linkContractToDepartment(2));


forkJoin(calls).subscribe(response_list=> { console.log(response_list) })
Sign up to request clarification or add additional context in comments.

5 Comments

From the question to me it is unclear if he wants to await multiple requests or if he wants to use the result from the first call in the second call. For the second case, switchMap should be used.
@CanK. : he has said I don't need to use directly the response in the next call. so there is no dependency of responses, so we can call all at once
@ShashankVivek I think it's pretty clear that switchmap needs to be used... "I need to wait for the call before to be completed, ALSO if I don't need to use directly the response in the next call."
I guess it would have been more clear if OP had written even if I don't need to use... But English isn't everyone's native or second language :)
Sometimes I need to use the response of the precedent call but often I don't need it, I also need all the response in the last subscribe so I can elaborate all
1

Like other said, Angular works perfectly with RxJS. But the official doc is a bit confuse. Try to find real world example with angular AND RXJS. Example : https://blog.angularindepth.com/practical-rxjs-in-the-wild-requests-with-concatmap-vs-mergemap-vs-forkjoin-11e5b2efe293

Learn rxjs is another nice ressource for RxJS

To resume:

  • To avoid subscribe of subscribe use Observable.
  • If multiple async call are independent use forkJoin
  • If an async call need previous async query use mergeMap
  • if async call need multiple async query use combineLatest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.