0

I using angular 2 http class https://angular.io/docs/js/latest/api/http/index/Http-class.html for sending post and get request. Now, I want to use send requests synchronously. i.e. second request is send after response of first request. But no option is available for it in angular 2 documentation. So,

How can I send multiple requests synchronously?

In jquery ajax async option is available which handle this type of problem. I am searching for similar option in angular 2.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Santab
  • 105
  • 7
  • Possible duplicate of [Angular 2: Two backend service calls on success of first service](http://stackoverflow.com/questions/36712659/angular-2-two-backend-service-calls-on-success-of-first-service) – eko Jan 20 '17 at 13:20
  • There is no such option in Angular2 – Günter Zöchbauer Jan 20 '17 at 13:29

1 Answers1

1

You can achieve synchronously / await with observables. there is many ways to get this example, this is one option. if you want more check: https://www.learnrxjs.io/operators/transformation/switchmap.html

You have also MergeMap and others

An example:

this.http.get(url1)
.switchMap(
 (response1: Response) => {
          return this.http.url(url2);
})
.subscribe(
   (response2: Response) => {},
   (error: Response) => {},
   () => console.log('completed')
);

Hope its helps you!