I’m working on an Electron app, and I’m trying to download multiple files concurrently from a folder structure using fs.promises.writeFile() inside an async function, but the files are downloading sequentially instead of concurrently, even though I’m using Promise.all(). Here’s my implementation:
const fs = require('fs');
const path = require('path');
async function downloadFolder(folderName, folderStructure, destinationPath) {
const folderPath = path.join(destinationPath, folderName);
if (!fs.existsSync(folderPath)) {
try {
fs.mkdirSync(folderPath, { recursive: true });
} catch (err) {
throw new Error(`Failed to create folder at ${folderPath}: ${err.message}`);
}
}
try {
const downloadPromises = folderStructure.map((item) => {
const itemPath = path.join(folderPath, item.name);
console.log(`Starting download for: ${itemPath}`);
if (item.type === 'file') {
return fs.promises.writeFile(itemPath, item.content)
.then(() => console.log(`Downloaded: ${itemPath}`))
.catch((err) => {
throw new Error(`Failed to write file at ${itemPath}: ${err.message}`);
});
} else if (item.type === 'folder') {
return downloadFolder(item.name, item.children, folderPath); // Recurse for subfolder
}
});
// Wait for all download promises to resolve concurrently
await Promise.all(downloadPromises);
console.log(`All items in ${folderName} downloaded successfully!`);
} catch (err) {
throw new Error(`Error processing folder ${folderName}: ${err.message}`);
}
}
However, when testing with a folder that only contains files (no nested folders) or with folders having nested folders, the downloads are happening sequentially, one after the other. Here’s an example of the folder structure I’m testing with:
folderStructure = [
{ name: '100-mb-example-jpg.jpg', type: 'file', content: /* file data */ },
{ name: '50mb.jpg', type: 'file', content: /* file data */ },
{ name: '70mb.iso', type: 'file', content: /* file data */ }
];
I expect the files to be downloaded concurrently, but they seem to be downloaded sequentially in the network. How can I fix this to download the files in parallel?