0

I want to return an array of promise using Q.all(); like this:

return Q.all([
              list[0].getCssValue('height'),
              list[1].getCssValue('height'),
              ...,
              list[list.length-1]
              .getCssValue('height')
             ]);

I need to return all list in the array and I can get the length by array.length.

The question is that I can't

   for(var i = 0; i < list.length; i++)

to make the return items like

list[i].getCssValue('height'); 

So how should I do this?

Flexo
  • 87,323
  • 22
  • 191
  • 272
coco
  • 1
  • 2

1 Answers1

0

Well, you actually can do it by creating a new array:

var arr = []
for(var i = 0; i < list.length; i++){
    arr.push(list[i].getCssValue('height');
}
return Q.all(arr); // wait for all getCssValue actions

However, there is a more elegant way with .map that maps each element from an array to another:

return Q.all(arr.map(function(x){ return x.getCssValue('height'); });
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504