リモートファイルアドレスの配列があります。foreach 配列に for を使用し、foreach の本体で、データ ダウンロード用の HTTP GET 要求を開始します。しかし、すべてが非同期であり、リクエストのコールバックでファイルを保存するには、ファイル名を知る必要があります。
これを解決するためのベストプラクティスは何ですか?
デモコード:
files = ["url.com/file.png", "url.com/file.doc"]
for file in files
req = http.get file, (response) =>
response.setEncoding 'binary'
body = ""
response.on "data", (chunk) =>
body += chunk
response.on "end", () =>
#Here I needs to know the file name to save it
fs.writeFileSync @currentFolder + "/Files/" + file, body, "binary"
ありがとうございました!