I am using Electron-dl
As per documentation
download function download(window,URL,Options)
accepts only url
and returns promise
Objective:
then ()
I want to get dl.getSavePath() paths for given arraycatch()
I want to get errors for failed items for given arrayWhat is wrong with code:
then()
is being called immediately instead of waiting for all downloads to finish
Zip File:
electron-dl-multi.zip
to use npm install && npm start
Big_array_test:
var files = [
'https://download.filezilla-project.org/client/FileZilla_3.34.0_win64-setup_bundled.exe',
'http://the.earth.li/~sgtatham/putty/latest/w32/putty-0.70-installer.msi',
'http://speedtest.ftp.otenet.gr/files/test10Mb.db'
]
Code:
var files= [
'http://speedtest.ftp.otenet.gr/files/test100k.db',
'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png'];
var promises = [];
var _browserWindow = BrowserWindow.getFocusedWindow();
//
for (var i = 0; i < files.length; i++) {
promises.push(download(_browserWindow, files[i], {
directory: os.tmpdir() // Default is User's downloads directory
}).then(function (dl) {
return dl.getSavePath();
}).catch(function (err) {
return err;
}));
}
Promise.all(promises)
.then(function (_files) {
console.log(_files);
}).catch(function (err) {
console.log( err);
});
I think your code is OK - except as what is said in comments, maybe you should not catch the error in promises.push
but instead just do it in Promise.all
.
I ran the app from your zip, and it worked and downloaded the 2 files correctly.
But then I tried to change some URL and put one that does not exist : well here's the problem. In this case download
function does not resolve the promise (which is normal) neither rejects it (which it should).
Try it yourself running this simple code :
download(mainWindow, 'https://nothing.wrong-url.org', {
directory: os.tmpdir() // Default is User's downloads directory
}).then(function (dl) {
console.log(dl.getSavePath());
}).catch(console.error)
The promise is just hanging here and does not resolve nor rejects. You might open an issue on electron-dl Github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With