0

I am working on a project where am converting the pdf to png with some async functions. it is working fine for document at a time, but when it comes to bulk documents the map iteration moving to next element from the array before completing a async function (which contains two more async function inside it). below is my code.

await Promise.all(batchInputFiles.map(async item => {
                    const fileData: any = await uploadService.PdfToPng(item.pdfPath, 'batch', item.pageCount, item.parentPath);
                }));
shafnaz
  • 19
  • 6
  • 1
    AFIK as I know `Promise.all` runs all actions in parallel, you wanna use `for of`, see this [answer](https://stackoverflow.com/a/46086037/1129986). Also you might want to edit your question a bit to make it easier to understand what is it that you need help with. – Bobby Tables Apr 14 '22 at 08:24
  • What I want is, the for loop should not continue until the pdfToPng process get completed fully. – shafnaz Apr 14 '22 at 09:45
  • you need to return promise from map function. for eg: `await Promise.all(batchInputFiles.map(async item => { return uploadService.PdfToPng(item.pdfPath, 'batch', item.pageCount, item.parentPath);}));` – deko_39 Apr 14 '22 at 09:47

1 Answers1

0
  1. map function need not be async function
  2. You are not returning the promise from the map function. Try this:
await Promise.all(batchInputFiles.map(item => uploadService.PdfToPng(item.pdfPath, 'batch', item.pageCount, item.parentPath)));