为了保持干净、简单和可维护的代码,我想在用户关闭消息框后退出函数......但是匿名函数是异步触发的,因此不会阻塞。 是否有实现此目标的最佳实践?我一直很喜欢 C# 的 MessageBox 实现......所以我也期望在 UI5 中实现这一点。好难过。这是代码...
} else { } */
MsgBox.error(info,
{
actions: [MsgBox.Action.OK], emphasizedAction: MsgBox.Action.NO,
onClose: function (sAction)
{
abort = true;
}
});
}
if(abort){
return;
}
this.clearLocalModel();
// code
// more code
// which is executed , but should not
// I KNOW, i can redesign this with if else... and so on- BUT
// i would like to keep it pretty simple...
在非常高的水平上,您可以创建类似以下函数的东西。它会呈现一个
MessageBox
,并根据用户点击的内容来解决或拒绝承诺。
askUserForInput: function() {
return new Promise((resolve, reject) => {
MessageBox.error(info, {
actions: [MsgBox.Action.OK],
onClose: (sAction) => {
if (sAction === MsgBox.Action.OK) {
resolve();
} else {
reject();
}
}
});
}
}
然后,您可以从其他地方调用此函数并等待结果(使用
then
或 async/await
,这两者都感觉更像是同步流程)。
handleFlow: async function() {
try {
await this.askUserForInput();
// Promise was resolved, do stuff (ideally call another method)
} catch (e) {
// Promise was rejected, do other stuff (ideally call another method)
}
}