[英]array files downloader with nodejs axios and async await pattern in vanilla javascript
我在香草 javascript 中編寫腳本並在 nodejs 中運行時遇到問題。 我想要得到的是:
// Get axios request
// response stream
// download file1..........finish
// When file is downloaded
// go to the next request
// Get axios request
// response stream
// download file2..........finish
// When file is downloaded
// go to the next request
我在節點和異步等待模式中編寫了這個腳本
// ARRAY OF BOOKS
ex. [{title: 'awesome book', link: 'http://www.example.com/awesome_book.pdf'},
{title: 'new book', link: 'http://www.example.com/new.pdf'} ...etc]
const Books = require('./Books');
const fs = require('fs');
const path = require('path');
const axios = require('axios');
// 1)
// loop the array of books and create array of links
let booksLinkinkArray = Books.map( book => book.link);
// 2)
// Function get request with axios
// response is stream
function request (element) {
try{
return axios({
url: element,
method: "GET",
responseType: "stream"
});
} catch(e) {
console.log( 'errore: ' + e)
}
}
// 3)
// Function that downloads the file
async function download(urls) {
urls.map(async url => {
try{
const saveFile = await request(url);
let file = url.split('/')[3];
console.log(file + ' : ' + 'init download');
let download = fs.createWriteStream(path.join(__dirname, 'books_dir', file));
saveFile.data.pipe(download);
console.log(file + ' : ' + 'downloaded')
} catch(e) {
console.log( 'error: ' + e)
}
}) }
download(booksLinkinkArray);
這個腳本沒問題,但是請求的循環太快了,文件下載重疊為:
// get request
// response
//download file init
// get request
// response
//download file init
ect...
file 1.......
file 2........
file 1...........
file 2..........
file 2.............. finish
file 1.............. finish
有沒有辦法管理 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 以便只有在 stream 中的所有塊都被耗盡后才能進行調用?
感謝您的回復
async download(urls){
for(const url of urls) {
const saveFile = await request(url);
const file = url.split('/')[3];
const download = fs.createWriteStream(path.join(__dirname, 'books_dir', file));
await new Promise((resolve, reject)=> {
saveFile.data.pipe(download);
download.on("close", resolve);
download.on("error", console.error);
});
}
}
async download(){ for(const url of urls) { ..... } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.