I'm struggling with getting a value from a closure function in Javascript. In my initial attempt, I placed the return statement inside the inner function, but it was not effective. Then, I tried moving the return statement to the outer function, however, the result still came back as undefined. Is there a reason why returnVal remains without a value when returned from the outer function even though it is assigned a value within the inner function?
console.log('Returned: ' + getColumn('CPC'));
function getColumn(header) {
var returnVal = undefined;
ptor.findElements(protractor.By.className('ngHeaderText')).then(function(headers) {
for (var i = 0; i < headers.length; i++) {
(function(i){
headers[i].getText().then(function(headerResult) {
if (headerResult == header) {
console.log('i: ' + i);
returnVal = i;
}
});
})(i);
}
});
return returnVal;
};
I would greatly appreciate any assistance on this matter!
EDIT: moved the return statement