0

I am calling several remote actions which I have to wait for to continue working with the code.

Therefore I created two arrays of promises which I will wait for and afterwards do some further code executions. Now the scope (context) is not showing the surrounding variables. So I think there is somehow a scoping or context issue in my code or JS understanding.

I already tried to understand the environment parameter of the function apply (this, arrayOfParam)

// Global variable
this._aAllSegments = [];
// local variable
var that = this;
var sRandomString = "testString"

var firstPromise = this.returnsSomePromise();
firstPromise.then(function(oDataFirst) {
   aAllPrePromises = [];
   aAllPromises = [];
   if(!that.loadOnceOne){
      aAllPrePromises.push(that.readExternalDataPromise());
   }‌
   if(!that.loadOnceTwo){‌
      aAllPrePromises.push(that.readExternalDataPromise());‌
   }‌
‌
   $.when.apply(null, aAllPrePromises).then(function() {‌
      var secondPromise = that.returnsSomePromise;‌
      secondPromise.then(function(oDataSecond) {‌
         aAllPromises.push(that.returnsSomePromise());‌
         for (var i = 0; i < that.aAllRounds.length; i++) {‌
            if (someComparison) {‌
               aAllPromises.push(that.returnsSomePromise());‌
            }‌
         }‌
         $.when.apply(null, aAllPromises).then(function() {‌
            //here I want to access the surrounding global and local variables‌
            that.doSomethingWithLocalAndGlobalVariables(that._aAllSegments, that.anotherFunction, sRandomString);‌
         });‌
      });‌
   });‌
});‌

I expected to be able to access as well global as local variable on top of the coding in the second $.when.apply scope, but it is only available withing the first $.when.applyscope.

Can someone explain this behavior and maybe how to correctly reorder this coding to get it running?

Even the sRandomString is not defined which makes no sense to me.

Inizio
  • 2,226
  • 15
  • 18
Mrebuh
  • 13
  • 1
  • 4
  • `this._aAllSegments` should be `that._aAllSegments` in last `$.when` due to different `this` context – charlietfl Jul 30 '19 at 15:10
  • Sorry for this error, but of course the this in the inner $.when.apply is a that. Was just a copy and past error. Question is still the same with now hopefully correct code :/ – Mrebuh Jul 30 '19 at 15:14
  • Can flatten these out a bit if you *return* `$.when` to next `then()` or *return* any promise to next `then()` in chain – charlietfl Jul 30 '19 at 15:27
  • Also can use `Promise.all()` instead of `$.when` since Promise API has support in all modern browsers – charlietfl Jul 30 '19 at 15:30
  • Thanks for the response, Promise.all() is not suitable as IE11 does not support it and this application needs IE11 support. For the flatten comment i do not really get what you are suggesting to change. – Mrebuh Jul 30 '19 at 16:24
  • Javascript's usual lexical scoping rules apply. Working with promises makes no difference in this regard. The inermost function can access the outermost members. – Roamer-1888 Jul 31 '19 at 02:16
  • Code in the question includes some 20 alien line feeds that mess up the syntax highlighting in my editor. They need to be purged. – Roamer-1888 Jul 31 '19 at 02:20
  • @Roamer-1888 so you mean in your opinion the code should run properly ? As I already mentioned before, I think so too, but it is not. So `sRandomString`, `that` or `that._aAllSegments` are not accessible inside of the second `$.when.apply` To the code formatting, for me it is working in notepad++ as well as in atom editor so not sure what errors or problems occure on your side. – Mrebuh Jul 31 '19 at 03:44
  • Promise.all() may not be available in IE11 natively, but it is polyfilled by UI5 so you can use it in any of your applications. Get rid of jQuery and it will be so much easier to fix your problem. – Marc Jul 31 '19 at 07:38
  • @MHuber, yes exactly so, scoping should not be an issue - it should run. – Roamer-1888 Jul 31 '19 at 14:07
  • @MHuber, try copying your code back from this page. You will find that the last 20 or so lines terminate in *Symbol: Zero Width Non-Joiner. Decimal Code: ‌* before the line break. They are really thin characters but will show in Notepad++ when highlighted. – Roamer-1888 Jul 31 '19 at 14:08

0 Answers0