![](/img/trans.png)
[英]How to await the response of an OData service's read operation in SAPUI5?
[英]How to wait for the response of an OData service's read operation in SAPUI5 before further execution of code?
我有一个 function(比如 function1),它在中途调用另一个 function(比如 function2)中的 OData 调用。 在函数 1 中的 OData 调用之后,我有一些过程,这取决于函数 2 中 OData 读取调用的响应。
目前,在function1执行过程中,调用function2时,会执行function2,但只有在function1的剩余行执行完后才会收到OData响应。 在执行 function1 的其余行之前,有没有办法在 function2 中等待 OData 调用的响应?
我对异步编程非常陌生。 但我尝试过的是:
function1: function() {
//some code
this.function2();
//some code & this is executed before success/error function of the OData call in function2
}
function2: async function() {
//oModel declaration
const oPromise = await new Promise((resolve, reject) => {
oModel.read("/entityset", {
success: (oData) => {
resolve(oData)
},
error: (oError) => {}
});
}).then((oData) => {
//some code
});
}
你的方向很好。 您应该在function1
中使用await
语句调用function2
。 这样做的先决条件是使function1
也async
:
function1: async function() {
//some code
await this.function2();
//some code & this will execute after success/error function of the OData call in function2
}
function2: async function() {
//oModel declaration
const oPromise = await new Promise((resolve, reject) => {
oModel.read("/entityset", {
success: (oData) => {
resolve(oData)
},
error: (oError) => {}
});
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.