I want to download images so I used Promise.
And my code snipped like that:
var download = function(uri, filename, callback) {
request.head(uri, function(err, res, body) {
/* console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']); */
request(uri).pipe(fs.createWriteStream(filename + '.' + res.headers['content-type'].split('/')[1])).on('close', callback);
});
};
var imagePaths = [];
for (const imageURL of imageURLs) {
imagePaths.push(new Promise((resolve, reject) => {
let filename = __dirname + '/../../../downloads/' + naming(6)
download(imageURL, filename, function(data, err) {
console.log("download image successful")
});
}))
}
Promise.all(imagePaths).then(() => {
console.log("11")
console.log(imagePaths)
});
And my output like that:
download image successful
download image successful
download image successful
And,
Promise.all(imagePaths).then(() => {
console.log("11")
console.log(imagePaths)
});
not working.
Why Im not getting imagePaths? How can I solve this?