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

前端 未结 5 2115
刺人心
刺人心 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条回答
  •  时光取名叫无心
    2020-12-17 19:48

    You should be able to concat to achieve sequence, and reduce to collect the emitted values:

    var a = this._newVersionService.createNewVersion(vnr);
    var b = this._versionService.getAvailableVersions(); 
    
    Rx.Observable.concat(a, b).reduce((acc:Array, x:any) => {
        acc.push(x); return acc;
    }, []).subscribe(t=> { 
          var firstEmitted = t[0];
          var secondEmitted = t[1];
    });
    

提交回复
热议问题