Quiero descargar imágenes, así que usé Promise.
Y mi código cortado así:
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) });Y mi salida así:
download image successful download image successful download image successfulY,
Promise.all(imagePaths).then(() => { console.log("11") console.log(imagePaths) });no funciona.
¿Por qué no obtengo imagePaths? ¿Como puedo resolver esto?