I am currently working on a project where I need to dynamically create multiple functions and run them in parallel.
My starting point is an array that contains several strings, each of which will be used as input for the functions. The number of functions matches the length of the array.
However, I have encountered an issue with my code and I'm unsure how to resolve it.
TS2345: Argument of type 'Promise' is not assignable to parameter of type '(data: string) => Promise'. Type 'Promise' does not match the signature '(data: string): Promise'.
I am seeking guidance on how to properly push the function into the list so that the return value can be stored in the data later on.
private async runParallel() {
let listOfFunctions: ((data: string) => Promise<string>)[] = [];
let outline: string[] = ["test1", "test2", "test3"]
for (let i = 0; i < outline.length; i++) {
let func = async (input: string): Promise<string> => {
// include async/await calls...
return input;
};
listOfFunctions.push(func(outline[i])); // TS2345 error occurs here
}
const data = await Promise.all(listOfFunctions);
console.log(data);
}