I have list values
xs = [1, 2, 3, 4]
I also have an asynchronous function squarethat returns the promise of the square of the argument passed to it. I want to pass the elements of my list to my function in parallel, and then collect the promises into an array and wait for them to complete. I can formulate this as a map / reduce operation.
Promise
.map(xs, function(x) {
return square(x)
}
)
.reduce(function(ys, y) {
return ys.concat(y)
}, [])
This ultimately returns the allowed values.
[1, 4, 9, 16]
Simple enough. Now say that I want to include the original argument in the answer array like this.
[{x:1, y:1}, {x:2, y:4}, {x:3, y:9}, {x:4, y:16}]
Now the difficult part I have is a list of objects, each of which at the beginning of the reduction step has a promise inherent in the property y. How to write Bluebird code to do this, reduce the step?