I need to call an async
function in a for
loop where every iteration 'builds' an object. At the end of the loop, this object should be resolved in a Promise
so that the next function in chain can pick it up for further processing. While the iterations get the data correctly, I am not able to collect the 'built up' object and return it. I keep getting {}
. Can you please help me resolve this?
async function imageDetailsCache(images) {
var imageData = {}; // This object is built up every iteration
var imageNo = '';
var promises = [];
//
for (let i = 0; i < images.length; i++) {
imageNo = images[i]
promises.push(
new Promise(function (resolve, reject) {
imageDetails(imageNo, 'BIG').then(function (result) {
imageData[imageNo] = { 'BIG': result }
imageDetails(imageNo, 'TINY').then(function (result) {
imageData[imageNo] = { 'TINY': result }
})
})
resolve(imageData)
})
)
}
Promise.all(promises).then(function (result) {
return result; // Always {}
})
}