3 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
使用rxjs來完成這項(xiàng)工作是一個(gè)非常好的解決方案。它易于閱讀嗎?我不知道。
另一種方法是執(zhí)行此操作并且更具可讀性(在我看來)是使用await / async。
示例:
async getContrat(){
//get the customer
const customer = await this.http.get('./customer.json').toPromise();
//get the contract from url
const contract = await this.http.get(customer.contractUrl).toPromise();
return contract; // you can return what you want here
}
然后叫它:)
this.myService.getContrat().then( (contract) => {
// do what you want
});
或者在異步功能中
const contract = await this.myService.getContrat();
您還可以使用try / catch來管理錯(cuò)誤:
let customer;
try {
customer = await this.http.get('./customer.json').toPromise();
}catch(err){
console.log('Something went wrong will trying to get customer');
throw err; // propagate the error
//customer = {}; //it's a possible case
}
- 3 回答
- 0 關(guān)注
- 767 瀏覽
添加回答
舉報(bào)