react-native 新手,目前我正在研究链式 promise 。
myFunction(human, destination = null) {
const { navigation } = this.props;
const { onRefresh } = navigation.state.params;
this.setState({ isLoading: true });
return PeopleService.closeService(
human.humanId,
destinationPoint && destinationPoint.humanId,
)
.then((result) => {
if (result) {
PeopleHelperService.refreshInfo().then(() => {
if (onRefresh) {
onRefresh();
}
navigation.popToTop();
PopUp.showSuccess(
"Success message",
);
});
}
PopUp.showError(
"Failing message",
);
return null;
})
.finally(() => this.setState({ isLoading: false }));
}
我想要实现的目标是消除链责任并使其变得简单而无需链接。
有人可以指导我如何实现这一目标吗?一些文档和其他来源的链接对于我了解如何制作它非常有帮助。
更新: 似乎是异步/等待工作的答案。
最佳答案
如果您不想使用 Promise,那么可以使用 async wait。在这里。
myFunction = async (human, destination = null) => {
const { navigation } = this.props;
const { onRefresh } = navigation.state.params;
this.setState({ isLoading: true });
let result = await PeopleService.closeService(
human.humanId,
destinationPoint && destinationPoint.humanId,
);
if (result) {
await PeopleHelperService.refreshInfo();
if (onRefresh) {
onRefresh();
}
navigation.popToTop();
PopUp.showSuccess(
"Success message",
);
}
PopUp.showError(
"Failing message",
);
this.setState({ isLoading: false })
}
关于javascript - 删除链式 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57321651/