Angular2 - How to chain async service calls (http requests) in a component?

前端 未结 5 2114
刺人心
刺人心 2020-12-17 18:59

I have a component which first need to call a service that POST something. Then in the same component I want to wait until the POST is done, to call another service which

5条回答
  •  -上瘾入骨i
    2020-12-17 19:38

    Better use switchMap() here.

    const versions$ = this._newVersionService.createNewVersion(vnr)
                     .switchMap(response => this._versionService.getAvailableVersions());
    
    versions$.subscribe(response2 => this.versions = response2)
    

    But the problem will be if you make another POST request before first has been resolved, the previous request will get cancelled.

提交回复
热议问题