Javascript 消除连锁承诺
新的反应,本地和目前我的工作链承诺Javascript 消除连锁承诺,javascript,reactjs,react-native,chaining,method-chaining,Javascript,Reactjs,React Native,Chaining,Method Chaining,新的反应,本地和目前我的工作链承诺 myFunction(human, destination = null) { const { navigation } = this.props; const { onRefresh } = navigation.state.params; this.setState({ isLoading: true }); return PeopleService.closeService( human.humanId,
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 })
}
您的代码逻辑可能有问题。如果
PeopleService.closeService()
承诺解析真实的结果
,则将显示两个弹出窗口。我猜。需要为finally块添加try catch
。我已经在尝试async/await,希望它能有所帮助,但当我按下closeSevice的按钮时,它只会创建无限加载。@AZ_u尝试catch?这有什么帮助?只是问一下,因为我想了解一些事情:)@somerk即使抛出错误,最终承诺块中的代码也必须执行,如果async wait
@AZ\uu中不包含try-catch块,这将不会发生。现在无限加载消失了,但我的弹出窗口似乎不想显示