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.