-1

The Function Promise.all - does have the job, to wait of asynchronious Functions and do certain logic after those asynchronious functions are done. I couldn't find how exactly does this function work i.e. if you can do certain things.

Example:

In this Code the Upperhalf till Promise.all, does fill the datelist with data. The Asynchronious function attachRequestCompleted - must first be done, so that datelist will be filled with data.

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

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

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Roman11222
  • 43
  • 5
  • 2
    `then(for ...` is invalid syntax regardless of which method you call. You cannot have a `for` loop when an expression is expected. – VLAZ Aug 12 '19 at 11:46
  • 1
    oModel.attachRequestCompleted is not a promise or a thenable object, thus you cannot use `Promise.all` – Marc Aug 12 '19 at 11:49
  • @Marc that's also true. In fact, since `attachRequestCompleted` seems to be a callback, the population of `datelist` appears to be asynchronous and thus the code given is not going to solve the problem. – VLAZ Aug 12 '19 at 11:51
  • Direction that i was going, since attachRequestCompleted is an asynchronious Function, i wanted it to complete first, so aftert the fact, an ensured datelist that is filled with content, can be used for further purposes. Proimise.all seemd to be the function, which kinda only des a certain action after the asynchronious function in Name, is done – Roman11222 Aug 12 '19 at 11:53
  • "*since attachRequestCompleted is an asynchronious Function*" it's not. It's a callback that will be *invoked* at some point but it's not itself async. – VLAZ Aug 12 '19 at 11:54
  • Well, how can i ensure that it is done in the first place, so that i can use datelist, which is now filled with content – Roman11222 Aug 12 '19 at 11:59
  • Why not add your code to the function given to `attachRequestCompleted`? That's the easiest option I can see. In fact, you can just *replace* the callback you give with whatever logic you want to perform. – VLAZ Aug 12 '19 at 12:02
  • i want to add the contents of datelist, as specialDates, but the Problem is, that it gives out an error that, 'this.byId' is not an function, essentialy, i can't target the Calendar, which is supposed to have the specialDates – Roman11222 Aug 12 '19 at 12:08

1 Answers1

0

As was indicated by the other comments, for the Promise function you would need to wrap the event callback in a 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. 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. 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.

With jQuery it would look like this:

var oRequest = jQuery.ajax({
  url: jLink
});
oRequest.done(function() {
  // this.byId(...)
}.bind(this));
pguddi
  • 325
  • 2
  • 3
  • 11
  • The jQuery namespace is deprecated, the UI5 core team is working hard on removing this unnecessary dependency. – Marc Aug 13 '19 at 06:16
  • Yes, but I guess it will be part of SAPUI5 for a long time to come. To be more future proof it's probably better to not use jQuery.ajax() but for beginners I find it easier since it is less verbose. – pguddi Aug 16 '19 at 16:24