我使用client.upload在pkgcloud上傳文件的目錄。所有流完成後執行回調的最佳方式是什麼?是否有內置的方法來註冊每個流的「完成」事件並在全部觸發後執行回調?等待所有流完成 - 流文件目錄
var filesToUpload = fs.readdirSync("./local_path"); // will make this async
for(let file of filesToUpload) {
var writeStream = client.upload({
container: "mycontainer,
remote: file
});
// seems like i should register finish events with something
writeStream.on("finish", registerThisWithSomething);
fs.createReadStream("./local_path/" + file).pipe(writeStream);
}
您可以使用async.js爲這種問題。您可以使用async.js提供的方法作爲'async.waterfall()'。一個函數的結果將作爲回調參數傳遞給另一個函數。您應該查看它的文檔。 –