I need to call 4 different API's on a single function which is need to execute one after another as my previous API result will use the next Call as a param. For example: I have a call API for geo Ip, then I need to call an another API which I need to pass lat and long which I have got from first API call.
How can I achieve this task on angular 4?
I heard about some methods like flatmap and forkjoin. Is this method can use? (I didn't have any code as I am little bit confused).
解决方案
If you can use es6 you can use async and await
async makeCalls() {
try {
let response1 = await this.http.get(firstEndpointUrl).toPromise();
let secondEndpointUrl = 'someUrl/' + response1.json().id;
let response2 = await this.http.get(secondEndpointUrl).toPromise();
let thirdEndpointUrl = 'someUrl/' + response2.json().id;
let response3 = await this.http.get(thirdEndpointUrl).toPromise();
return response3.json();
} catch (error) {
// handle error
}
}