3 回答
TA贡献1809条经验 获得超8个赞
使用rxjs来完成这项工作是一个非常好的解决方案。它易于阅读吗?我不知道。
另一种方法是执行此操作并且更具可读性(在我看来)是使用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来管理错误:
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 关注
- 735 浏览
添加回答
举报