kevva / download

Download and extract files
MIT License
1.28k stars 200 forks source link

Downloading with Promise.all will not let me name the files while downloading #180

Open CognitiveClouds-Prasad opened 5 years ago

CognitiveClouds-Prasad commented 5 years ago

I was having the issue of downloading the files according to the file-name I wish to name.

In the following, I do not have the ability to pass the destination file names as an array or so. Promise.all([ 'unicorn.com/foo.jpg', 'cats.com/dancing.gif' ].map(x => download(x, 'dist'))).then(() => { console.log('files downloaded!'); });

I tried passing as an array in the download() function. However, it downloads with the original file names and does not throw any error either.

aymather commented 5 years ago

Not necessarily as an array, but you can restructure your array to have titles. Here's an example...

var arr = [ {uri: 'unicorn.com/foo.jpg', title: 'Example title 1'}, {uri: 'cats.com/dancing.gif', title: 'Example title 2'} ]

Now pass that into Promise.all() like so...

Promise.all(arr.map(arr_item => { download(arr_item.uri, (process.cwd() + '/'), {filename: arr_item.title}) })) .then(() => { console.log('meow'); })