I want to wait for an array of Promises but I cannot add them without executing them. So I had to create a wrapper function, however Promise.all does not execute them.
This is my code:
const set = new Set(Array.from([1, 2, 3, 4, 5]));
const func = async (input) => {
let result = await asyncFunc(input);
return result;
}
var promises = [];
for(const element of set) {
promises.push(() => { func(element) });
}
await Promise.all(promises).then(values => {
console.log(values);
});
I exepect Promise.all executes the func function, but it does not. How can I achieve this result?
You could just push promises to your array, not functions:
const set = new Set(Array.from([1, 2, 3, 4, 5]));
const func = async (input) => {
let result = await asyncFunc(input);
return result;
}
var promises = [];
for(const element of set) {
promises.push(func(element));
}
await Promise.all(promises).then(values => {
console.log(values);
});
I found my problem!
The issue of mine was that some of my promises will reject and Promise.all does not continue when this is the case!
I solved by using Promise.allSettled!