Answer the question
In order to leave comments, you need to log in
Is it possible to return a function for each element of an array and call it in Promise.all?
Hello!
Tell me if it is possible to do this: I have an array of values, for each element of the array I need to go through and return an asynchronous function, inside which a request to the server will be executed (the parameter is the array element itself). In the future, I would like to wrap the result in Promise.all () so that the requests go in parallel. Is it possible?
My code that doesn't work:
const filePromises = filesToDelete.map((file: DocsManagerFile) => () => {
httpService.sendDataTest(
{ data_in: { id: file.id } },
'physdocs.delete_docs_file',
);
});
await Promise.all(filePromises);
Answer the question
In order to leave comments, you need to log in
If the method httpService.sendDataTest(...)
returns a promise, then you should not wrap it in an arrow function, like this:
filesToDelete.map((file: DocsManagerFile) => httpService.sendDataTest({ ... }))
filesToDelete.map((file: DocsManagerFile) => (
new Promise(onsuccess, onerror) => (
httpService.sendDataTest(
{ data_in: { id: file.id } },
'physdocs.delete_docs_file',
// где-то тут onsuccess и onerror
);
))));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question