Running Node v 8+, along with bluebird and lodash libraries.
Here is an array of fake objects:
const theList = [
{name: 'foo'
values: [ promise, promise, promise ] },
{name: 'bar',
values: [ promise ] }
];
How do i write promise resolvers such that I can iterate the entire array of objects, and resolve the property containing the array of promises? Each promise will resolve as an array of objects... thus it could wind up looking like: [ [resolvedObj1], [resolvedObj1,resolvedObj2,resolvedObj3], [resolvedObj1, resolvedObj2] ]
I'd like them all to resolve such that I have an object of the following:
let resolved = [
{name: 'foo',
values: [resolved, resolved, resolved, resolved, resolved]},
{name: 'bar',
values: [resolved]}
];
Here was my current solution:
const resolvedCollection= await Promise.map(theList, async obj=> {
const resolved = await Promise.all(obj.values);
obj.values = _.flatten(resolved); // lodash "flatten" function
return obj;
});
Is there a cleaner way to do this? Also maybe a way in which I need not hardcode the values property of each object iterated on?