我有一个组件,首先需要调用POST的服务.然后在同一个组件中我想等到POST完成后,调用另一个获取数据的服务.
在new-version.component.ts中:
private createNewVersion(value) { ... // create new version,then call on all available versions // POST call this._newVersionService.createNewVersion(vnr); // GET call this._versionService.getAvailableVersions(); ... }
在new-version.service.ts中:
export class NewVersionService { response$: Subject<any>; constructor(private _http: Http) { this.response$= new BehaviorSubject<any>(null); } public createNewVersion(versionNr) { this._http.post('http://localhost:8080/services/' + versionNr,null,{ method: 'POST',}) .subscribe(response => { this.response$.next(response.status); },error => console.error(error)); }
谢谢!
当一个调用返回一个Promise链时调用
someFunction() { return returnsPromise() .then(result => doSomethingNext()) .then(result => doSomethingAfterThat()); }
确保你有一个返回该链的Promise的返回值,因此someFunc()的调用者也有机会在doSomethingAfterThat()完成后执行额外的工作.
当一个调用返回一个Observable时,然后使用完整的回调
someFunction() { return returnsObservable() .subscribe( event => doForEachEvent(),error => handleError(),() => doSomethingNext() .then(result => doSomethingAfterThat()); }
doSomethingNext()在最后一个事件之后执行,doSomethingAfterThat()再次与then()链接以显示如何混合observable和promise.做一点事().