【问题标题】:Electron-dl use promise.all for multiple simultaneous downloadsElectron-dl 使用 promise.all 进行多个同时下载
【发布时间】:2018-07-05 05:55:28
【问题描述】:

我正在使用Electron-dl 根据documentation 下载功能download(window,URL,Options)只接受url 并返回承诺

目标:

  1. 我想同时下载文件数组
  2. then () 我想获取给定数组的dl.getSavePath() 路径
  3. catch() 中,我想为给定数组的失败项目获取错误
  4. promise.all 可以做这项工作吗?还有其他选择吗?

代码有什么问题: then() 被立即调用,而不是等待所有下载完成

压缩文件:

electron-dl-multi.zip
使用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'
  ]

代码:

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);
        });

【问题讨论】:

  • 如果你在Promise.all之前发现了错误,你会莫名其妙地吞下可能的错误,你确定没有任何错误吗?
  • @YoukouleleY 对我来说,主要问题是没有等待所有下载完成。让我在某处制作测试样本
  • 附加的压缩文件
  • 我认为 Electron-dl 的承诺是错误的 ??

标签: javascript node.js electron


【解决方案1】:

我认为你的代码没问题 - 除了 cmets 中所说的,也许你不应该在 promises.push 中发现错误,而应该在 Promise.all 中进行。

我从您的 zip 中运行了该应用程序,它运行正常并正确下载了 2 个文件。

但后来我尝试更改一些 URL 并放置一个不存在的 URL:这就是问题所在。在这种情况下,download 函数不会解决承诺(这是正常的),也不会拒绝它(它应该)。

自己尝试运行这个简单的代码:

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)

promise 只是挂在这里,既不解决也不拒绝。您可能会在 electron-dl Github 上打开一个问题。

【讨论】:

  • 但是如果你使用像 10mb 这样的文件,那么 () 会比等待下载完成更早地被调用。你能验证一下吗
  • @django 我也注意到了,在最后的数组中,两个名字是一样的,看起来像 download 函数故障
猜你喜欢
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 2014-03-02
  • 1970-01-01
  • 2023-04-01
  • 2019-04-26
  • 2016-11-23
相关资源
最近更新 更多