Reputation: 3625
Currently I am using a npm package called download to download files. I am looping through an array of links to download with a forEach and the download is being triggered within the loop. The problem I am having is when the array of links to download is quite big, the files aren't completely being downloaded. I think this is happening because I am downloading so many files asynchronously. So now I am trying to do this synchronously, but I can't seem to find how to do this. In the snippet below I am only showing the relevant code that is needed for my problem.
arrayLinks.forEach(function(link){
new download({mode: "755"}).get(link).rename(title).dest(directory).use(downloadStatus()).run();
});
Upvotes: 1
Views: 2868
Reputation: 5055
Best way is to write it in a function and use the callback function of run() to fetch the next file from the huge file list.
function loadFiles(fileList){
if(fileList instanceof Array && fileList.length > 0){
new download({
mode:"755"
})
.get(fileList.splice(0,1)[0]) // Fetches the first element from array
.rename(title)
.dest(directory)
.use(downloadStatus())
.run(function(){
loadFiles(fileList);
});
}else{
console.log('job done');
}
}
loadFiles(originFileList);
When you want to run multiple downloads the function can be called in a loop and give them all the same fileList.
for(var i=0;i<10;i+=1){
loadFiles(originFileList); // Will run 10 downloads in parallel until fileList is empty.
}
Upvotes: 1