NodeJs 使用 async with for 循环下载
NodeJs Use async with for loop download
我有这个循环来下载一些文件,它工作正常。
但是文件的下载顺序是“2,3,4,1,5”,而不是“1,2,3,4,5”。
我知道如何执行 .each 异步和瀑布异步,但我不知道如何执行此循环。
Config.TotalFiles = 5;
for(i = 1; i <= Config.TotalFiles; i++) {
$this.CreateJSONFile(i, function() {
cls();
});
}
下载完成后,我想调用我的回调,我试过这个if(id == Config.TotalFiles)
,但它不起作用,因为 id 不好。
如何使用此循环完成 "async" 过程?
谢谢
您可以为此使用 async.whilst:
Config.TotalFiles = 5;
var count = 1;
//pass your maincallback that you want to call after downloading of all files is complete.
var callMe = function(mainCallback){
async.whilst(
function() { return count <= Config.TotalFiles; },
function(callback){
$this.CreateJSONFile(count, function() {
cls();
count++;
callback();
});
}, function(){
//This function is the final callback
mainCallback();
})
}
是的,您需要实现异步 for 循环。主要思想是在当前步骤完成时调用回调(如继续)。您可以使用上面提到的一些有用的库 (async.js)。但是你也必须明白它不会并行完成(所以完成下载的时间会增加)。
这些很好的例子可以帮助你:https://github.com/caolan/async/blob/v1.5.2/README.md#whilst
请检查我的屏幕以了解我的情况。
或者这个 pastebin https://pastebin.com/jtANeSxq
cls();
for(i = 1; i <= Config.TotalFolders; i++) {
$this.CreateJSONFile(i, function() {
setTimeout(function() {
cls();
}, 200);
});
}
CreateJSONFile(id, callback) {
console.log(chalk.hex("#607D8B")(`Creating JSON Files... [${id}/${Config.TotalFolders}]`));
var requestOptions = {
encoding: "binary",
method: "GET",
uri: `${Config.Url.Source}${Config.Url.Audio}` + `${id}/skeud.json`
};
request(requestOptions, function (error, response, body) {
if (!error && response.statusCode === 200) {
if(!fs.existsSync(Config.DownloadsPath + `${id}/skeud.json`)) {
fs.writeFile(Config.DownloadsPath + `${id}/skeud.json`, body, function(err) {
if(err) {
return console.log(err);
}
console.log(chalk.hex("#FFEB3B")(`JSON File ${chalk.inverse(id)} ${chalk.hex("#FFEB3B")("created")}`));
});
} else {
console.log(chalk.hex("#FFEB3B")(`JSON File ${chalk.inverse(id)} ${chalk.hex("#FFEB3B")("skipped")}`));
}
}
});
/*if(created == Config.TotalFolders) {
callback();
}*/
}
谢谢
我有这个循环来下载一些文件,它工作正常。
但是文件的下载顺序是“2,3,4,1,5”,而不是“1,2,3,4,5”。
我知道如何执行 .each 异步和瀑布异步,但我不知道如何执行此循环。
Config.TotalFiles = 5;
for(i = 1; i <= Config.TotalFiles; i++) {
$this.CreateJSONFile(i, function() {
cls();
});
}
下载完成后,我想调用我的回调,我试过这个if(id == Config.TotalFiles)
,但它不起作用,因为 id 不好。
如何使用此循环完成 "async" 过程?
谢谢
您可以为此使用 async.whilst:
Config.TotalFiles = 5;
var count = 1;
//pass your maincallback that you want to call after downloading of all files is complete.
var callMe = function(mainCallback){
async.whilst(
function() { return count <= Config.TotalFiles; },
function(callback){
$this.CreateJSONFile(count, function() {
cls();
count++;
callback();
});
}, function(){
//This function is the final callback
mainCallback();
})
}
是的,您需要实现异步 for 循环。主要思想是在当前步骤完成时调用回调(如继续)。您可以使用上面提到的一些有用的库 (async.js)。但是你也必须明白它不会并行完成(所以完成下载的时间会增加)。
这些很好的例子可以帮助你:https://github.com/caolan/async/blob/v1.5.2/README.md#whilst
请检查我的屏幕以了解我的情况。
或者这个 pastebin https://pastebin.com/jtANeSxq
cls();
for(i = 1; i <= Config.TotalFolders; i++) {
$this.CreateJSONFile(i, function() {
setTimeout(function() {
cls();
}, 200);
});
}
CreateJSONFile(id, callback) {
console.log(chalk.hex("#607D8B")(`Creating JSON Files... [${id}/${Config.TotalFolders}]`));
var requestOptions = {
encoding: "binary",
method: "GET",
uri: `${Config.Url.Source}${Config.Url.Audio}` + `${id}/skeud.json`
};
request(requestOptions, function (error, response, body) {
if (!error && response.statusCode === 200) {
if(!fs.existsSync(Config.DownloadsPath + `${id}/skeud.json`)) {
fs.writeFile(Config.DownloadsPath + `${id}/skeud.json`, body, function(err) {
if(err) {
return console.log(err);
}
console.log(chalk.hex("#FFEB3B")(`JSON File ${chalk.inverse(id)} ${chalk.hex("#FFEB3B")("created")}`));
});
} else {
console.log(chalk.hex("#FFEB3B")(`JSON File ${chalk.inverse(id)} ${chalk.hex("#FFEB3B")("skipped")}`));
}
}
});
/*if(created == Config.TotalFolders) {
callback();
}*/
}
谢谢