【发布时间】:2020-05-21 18:58:44
【问题描述】:
您好,在 ionic3 应用程序中,我的代码取决于 2 个提供者 getChildren 和 getBottle - 哪些提供者返回承诺。我想让这段代码干净,但是我需要使用 2 个提供程序来处理来自 API 的数据。感谢您的任何建议。
this.dayReportProvider.getChildren(this.groupId, this.dateFromDailyOverview)
.then((val: any) => {
this.fetchedChildren = val.mobile_employee_getactive_children_of_day.map(item => item);
this.fetchedChildren.forEach((item, i) => {
this.fetchedChildren[i].color = "secondary"
})
return this.fetchedChildren;
})
.then((fetchedChildren) => {
console.log('second then fetchedChildren =>', this.fetchedChildren)
// calling the second Provider
return this.getBottle();
})
.then((preselectedChildren) => {
console.log('third then after getBottle() preselectedChildren', preselectedChildren);
this.preselectedChildren = preselectedChildren;
this.fetchedChildren = this.fetchedChildren.map((el) => {
if (this.preselectedChildren.includes(Number(el.id))) {
return {
...el,
color: 'primary'
}
}
return el;
});
// Make preselectedChildren available for submit
this.selectedChildren = this.fetchedChildren.filter((child) => {
return child.color === "primary";
})
if (this.navParams.data.childId) {
this.childId = this.navParams.data.childId;
this.selectChildBasedOnParams();
}
this.appFunctionCtrl.dismissLoader();
})
.catch((err) => console.log('Errror with fetching children', err))
【问题讨论】:
标签: angular typescript ionic3