Reputation: 1
I have an http service with a method like this:
get(url): Promise<Object> {
//await something here before next line
return this.httpClient.get(url)
.map((res) => {
return res as Object
})
.toPromise();
I need to call a method that eventually perform some object updates. How do I make the above method wait until the other is finished?
Upvotes: 1
Views: 811
Reputation: 1
This is how I solved it:
get(url): Promise<Object> {
return this.sendRequest(url, 'get', '')
.map((res) => {
return res as Object
})
.toPromise();
}
sendRequest(url, type, body): Observable<any> {
return this.adalService.acquireToken()
.flatMap((token: string) => {
//I am not using the result of the flatmap, only exploiting it to ensure resolution, httpClient is being intercepted with the updated values
return this.httpClient[type](url, body)
});
}
Upvotes: 0
Reputation: 9330
This can be done many ways. One of them is forkJoin
( which is similar to promise.all
)
// parallel method you want to execute , let this return an observable
doSomething(){
return this.http.get('token'.... );
}
get(url): Promise<Object> {
return Observable.forkJoin([doSomething(),this.httpClient.get(url)])
.map((res) => { //res is an array with both outputs
// we are only concerned with second response
return res[1] as Object;
})
.toPromise();
Upvotes: 2