I need to download all images and generate word document with them. Using nodeJS and Meteor
WebApp.connectHandlers.use('/download', async function (req, res, next) {
// ...
const images = [];
await lines.forEach(async (line, k) => {
if (line.type && line.type === 'image') {
images.push({
id: line.id,
file: line.id + '.jpg',
});
download_image(line.imageUrl, line.id + '.jpg');
}
});
// ...
// Then I use images[] to insert them into a Word document.
});
const download_image = (url, image_path) =>
axios({
url,
responseType: 'stream',
}).then(
(response) =>
new Promise((resolve, reject) => {
response.data
.pipe(fs.createWriteStream(image_path))
.on('finish', () => resolve())
.on('error', (e) => reject(e));
})
);
The problem is images are not getting downloaded before I insert the into a Word document.
How to stop/await before images are finished to be downloaded? I am not so good with promises. What is missing her?
Thanks!