How can i tell if the below code has finished for it to resolve the promise for the next task to happen
It currently downloads around ~500 files and saves them to disk, but how can i tell once the last file has been written to disk to resolve the promise?
exports.DownloadImages = function (provider, data) {
return new Promise(function (resolve, reject) {
data.forEach(function (vehicle) {
var images = vehicle.Gallery;
images.forEach(function (image) {
const url = transformation.replaceAll(image, '[size]', 'original');
const filename = transformation.replaceAll(transformation.ImageFilename(url), '[size]', 'original');
const writer = fs.createWriteStream('./export/' + provider + '/' + filename);
api.get(url, {
responseType: 'stream',
httpsAgent: new https.Agent({ keepAlive: true })
}).then((response) => {
response.data.pipe(writer);
})
.catch((error) => {
console.error('[HELPER] DownloadImages ' + error + ' on Image ' + url);
});
});
});
// Need to check to see if all data has been processed...
resolve();
});
};