Multi Threaded Generation
JavaScript and Node.js both support asynchronous processing. So does IronPDF, a .NET PDF library, known for its efficient PDF generation and manipulation capabilities.
Most of IronPDF's methods return promises. Leveraging this, we can render multiple HTML contents, and then use the Promise.all
method to wait until all the promises have finished.
Please note that the Promise.all
method does not guarantee the success of all promises; it only ensures that the promises have finished.
The await
within the .map
function is used to wait for the returned PDF documents and perform further edits on the documents. If your goal is solely to render the PDFs without additional processing, the use of async
and await
may not be necessary.
// Example of using Promise.all with IronPDF's asynchronous methods
// Assuming we have an array of HTML content strings
const htmlContents = [
'<h1>Document 1</h1>',
'<h1>Document 2</h1>',
'<h1>Document 3</h1>'
];
// Function to simulate rendering HTML to PDF asynchronously
async function generatePdfFromHtml(html) {
// Simulate asynchronous PDF generation
return new Promise((resolve) => {
setTimeout(() => {
const pdfDocument = `PDF generated from: ${html}`;
resolve(pdfDocument);
}, 1000); // Simulate some delay for PDF rendering
});
}
(async () => {
try {
// Map over each HTML content and convert them to PDFs concurrently
const pdfPromises = htmlContents.map(async (html) => {
// Await the PDF generation for each document
const pdfDocument = await generatePdfFromHtml(html);
console.log(`Successfully generated: ${pdfDocument}`);
return pdfDocument; // Return the generated PDF document
});
// Wait for all PDF generation promises to resolve
const pdfDocuments = await Promise.all(pdfPromises);
console.log('All PDFs generated successfully:', pdfDocuments);
} catch (error) {
console.error('Error generating PDFs:', error);
}
})();
// Example of using Promise.all with IronPDF's asynchronous methods
// Assuming we have an array of HTML content strings
const htmlContents = [
'<h1>Document 1</h1>',
'<h1>Document 2</h1>',
'<h1>Document 3</h1>'
];
// Function to simulate rendering HTML to PDF asynchronously
async function generatePdfFromHtml(html) {
// Simulate asynchronous PDF generation
return new Promise((resolve) => {
setTimeout(() => {
const pdfDocument = `PDF generated from: ${html}`;
resolve(pdfDocument);
}, 1000); // Simulate some delay for PDF rendering
});
}
(async () => {
try {
// Map over each HTML content and convert them to PDFs concurrently
const pdfPromises = htmlContents.map(async (html) => {
// Await the PDF generation for each document
const pdfDocument = await generatePdfFromHtml(html);
console.log(`Successfully generated: ${pdfDocument}`);
return pdfDocument; // Return the generated PDF document
});
// Wait for all PDF generation promises to resolve
const pdfDocuments = await Promise.all(pdfPromises);
console.log('All PDFs generated successfully:', pdfDocuments);
} catch (error) {
console.error('Error generating PDFs:', error);
}
})();