Reputation: 240
What I am looking to do is to use an array of strings to use those values to map a new array of promise functions for a Promise.all argument.
I think explains my thought process so far, as well as the issue I am running into.
const strings = ['A', 'B', 'C'] // Varies in size
const stringFunctions = strings.map(item => {
const func = () => {
promised(item) // Returns Promised Boolean
}
return func
})
const PromiseAll = new Promise(function(resolve, reject) {
Promise.all(stringFunctions)
.then(items => {
const obj = {};
items.forEach((item, index) => {
obj[strings[index]] = item;
});
resolve(obj);
// returns { A: func(), B: func(), C: func() }
// expected { A: bool, B: bool, C: bool }
})
.catch(error => {
reject(error);
});
}
Upvotes: 2
Views: 1539
Reputation: 62676
This can be done without any explicit promise creation (you have all you need in the promised
function and in Promise.all()
).
let strings = [ ... ]
let promises = strings.map(string => promised(string));
Promise.all(promises).then(results => {
// results is a new array of results corresponding to the "promised" results
});
Upvotes: 2