0

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;
        });
bob.mazzo
  • 5,183
  • 23
  • 80
  • 149
  • 1
    getText returns a promise that will not return your convertCellData back up again. So you have to use several promises to get a result. – Blauharley Feb 02 '17 at 19:27
  • @Blauharley - thanks, as that did cross my mind earlier but I forgot to revisit that. Perhaps I need the `.then()` after all (since the latter version does work fine) – bob.mazzo Feb 02 '17 at 19:29
  • Thats right or a callback function but there are just two options left here – Blauharley Feb 02 '17 at 19:29

0 Answers0