【发布时间】:2018-07-05 05:55:28
【问题描述】:
我正在使用Electron-dl
根据documentation
下载功能download(window,URL,Options)只接受url
并返回承诺
目标:
- 我想同时下载文件数组
- 在
then ()我想获取给定数组的dl.getSavePath() 路径 - 在
catch()中,我想为给定数组的失败项目获取错误 - 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