[英]Promise All Syntax and the Frame of accepatble Operations
The Function Promise.all - does have the job, to wait of asynchronious Functions and do certain logic after those asynchronious functions are done. Function Promise.all-确实有工作,等待异步功能并在完成这些异步功能后执行某些逻辑。 I couldn't find how exactly does this function work ie if you can do certain things.
我找不到该功能的工作原理,即您是否可以做某些事情。
Example: 例:
In this Code the Upperhalf till Promise.all , does fill the datelist with data. 在此代码中,直到Premise.all的Upperhalf确实用数据填充了日期列表。 The Asynchronious function attachRequestCompleted - must first be done, so that datelist will be filled with data.
异步函数attachRequestCompleted-必须首先完成,以便日期列表中将填充数据。
Within Promise.all i want to iterate through the datelist which was filled with data in attachRequestCompleted, so that i can later add them as Special Dates in the Calendar 在Promise.all中,我想遍历在attachRequestCompleted中填充了数据的日期列表,以便以后可以将它们添加为Calendar中的特殊日期。
var datelist = [];
var oModel = new sap.ui.model.json.JSONModel();
console.log(oModel.oData, datelist.length, datelist);
oModel.attachRequestCompleted(function() {
var oFeiertageBerlin = oModel.getData().BE;
for (var prop in oFeiertageBerlin) {
datelist.push(oFeiertageBerlin[prop].datum);
}
});
var jDatum = new Date();
var jLink = "https://feiertage-api.de/api/?jahr=" + jDatum.getFullYear();
oModel.loadData(jLink);
Promise.all([
this.oModel.attachRequestCompleted
]).then(
for (var j = 0; j < datelist.length; j++) {
console.log(datelist[j]);
}
)
Expected Result: Possibility to iterate through the List 预期结果:遍历列表的可能性
Actual Result: Syntax Error 实际结果:语法错误
As was indicated by the other comments, for the Promise function you would need to wrap the event callback in a Promise. 如其他注释所示,对于Promise函数,您需要将事件回调包装在Promise中。 However, your last comment lets me to believe that your root issue with the "this.byId" is that you'll need to bind the context for the callback.
但是,您的最后一条评论使我相信,“ this.byId”的根本问题是您需要为回调绑定上下文。 Hence:
因此:
oModel.attachRequestCompleted(function() {
// now this.byId(...) should work here
}.bind(this));
However, I would propose not using this setup and avoid the loadData function of the JSON model. 但是,我建议不要使用此设置,而应避免使用JSON模型的loadData函数。 The attaching of event handlers for completed / failed etc seems not be very elegant for me.
对于完成/失败等事件处理程序的附加对我来说似乎不是很优雅。 I would rather go for jQuery.ajax() ( https://api.jquery.com/jQuery.ajax/ ) or a manual XMLHttpRequest.
我更愿意使用jQuery.ajax()( https://api.jquery.com/jQuery.ajax/ )或手动XMLHttpRequest。
With jQuery it would look like this: 使用jQuery,它看起来像这样:
var oRequest = jQuery.ajax({
url: jLink
});
oRequest.done(function() {
// this.byId(...)
}.bind(this));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.