Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron-dl use promise.all for multiple simultaneous downloads

I am using Electron-dl As per documentation download function download(window,URL,Options) accepts only url and returns promise

Objective:

  1. I want to download array of files simultaneously
  2. in then () I want to get dl.getSavePath() paths for given array
  3. in catch() I want to get errors for failed items for given array
  4. Could promise.all do this job ? Any other alternative ?

What 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);
        });
like image 299
django Avatar asked Jul 05 '18 05:07

django


1 Answers

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.

like image 188
Air1 Avatar answered Nov 14 '22 08:11

Air1