简体   繁体   中英

Async option in http request in angular 2

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. ie 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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM