Saturday, 4 March 2017

Download an array of files and wait until they are done

I'm a python guy, so JS's asynchronous nature is odd to me and it seems like doing something relatively simple requires some gymnastics.I have an array of filenames on a remote server. I want to download each of these files, then push to an array whether or not it was successful.The included modules are: http://ift.tt/2lLUNve``` var path = require('path'); var download = require('download-to-file'); var async = require('async');var failed = []; var success = [];async.each(filename_array, function(filename){ remote_path = 'http://ift.tt/2mFa4Cx'+filename; local_path = '/webdownloads/'+filename; download(remote_path, local_path, function(err){ if(err){ failed.push(filename); } else { success.push(filename); } }); });console.log(failed) console.log(success) ```The files download correctly, but the output is:[] []How can I pull this off? I need to wait until all of the files have either downloaded or failed before the next step can be initiated, but the script doesn't wait for the downloads.

Submitted March 05, 2017 at 03:18AM by nosmokingbandit

No comments:

Post a Comment