How to make a synchronous call in angular 5?

后端 未结 5 1225
我寻月下人不归
我寻月下人不归 2021-01-11 11:33

So, I was trying to get the solution of this problem. But, somehow I am unable to do so, May be because of the lack of knowledge in angular 5. This is my service:

         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-11 12:35

    To make sure async calls are executed before processing response you may use Observable.forkJoin() from ReactiveX.

    import {Observable} from 'rxjs/Observable';
    import 'rxjs/add/observable/forkJoin';
    
    Observable.forkJoin(
        this.http.get('/links.json').map((response:Response) => response.json()),
        this.http.get('/bookmarks.json').map((response:Response) => response.json())
    ).subscribe(
        data => {
          this.links = data[0]
          this.bookmarks = data[1]
        },
        error => console.error(error)
    );
    

    subscribe() function's onNext handler will execute when all HTTP requests complete successfully.

提交回复
热议问题