My confusion with my array.map()
example is that it returns an array of undefined
values.
This is in a Protractor
e2e context, but very specific to array.map
this.getColData = function(colIdx){
var colClass = '.columnUi-' + colIdx;
var cols = element.all(by.css(colClass));
var that = this;
return cols.map(function(elem, idx){
elem.getText().then(function(txt){
console.log(' COL TEXT: ' + that.convertCellData(txt)); // PRINT CORRECT VALUES
return that.convertCellData(txt);
});
}
}
And the caller is :
pageObj.getColData(colIdx).then(function(colAry){
for (var i = 0; i < colAry.length; i++) {
console.log('--------> [' + i + '] = ' + colAry[i]); // ALL VALUES UNDEFINED
}
}
Inside getColData()
, the console.log()
prints the correct values; however back in the caller's .then()
promise resolution they are all undefined
---- COLUMN COUNT = 8
COL TEXT: 0
COL TEXT: 0
COL TEXT: 0
COL TEXT: -2565
COL TEXT: 8154
COL TEXT: 5589
COL TEXT: 5589
COL TEXT: 5589
--------> [0] = undefined
--------> [1] = undefined
--------> [2] = undefined
--------> [3] = undefined
--------> [4] = undefined
--------> [5] = undefined
--------> [6] = undefined
--------> [7] = undefined
if I write it this this, however, it returns the new array successfully:
return cols.map(function (elem, idx) {
elem.getText().then(function (txt) {
console.log(' COL TEXT: ' + that.convertCellData(txt));
colAry.push(that.convertCellData(txt));
});
browser.sleep(200);
})
.then(function () {
return colAry;
});