angular自带的一些api,如何使用Angular在一个函数上调用多个API?

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值