I am currently working on a project using Angular 7. I have a function that contains a promise which saves the result in an array as shown below:
appendImage(item){
this.imageCompress.compressFile(item, 50, 50).then(
result => {
this.compressedImages.push(result);
return this.compressedImages;
});
}
Now, I am calling this function/promise from another function that includes a for loop:
async compressFiles() {
if(this.elementsSelected.length > 0){
for(let i = 0; i < this.elementsSelected.length; i++){
let actual = this.elementsSelected[i].src;
let res = await this.appendImage(actual);
console.log(res);
}
return this.compressedImages;
}else{
console.warn("No Images Selected");
}
}
My goal is to return the updated array once all promises are resolved. While printing the array directly within the appendImage()
function shows it as expected, when I log res
inside the loop, I get undefined
.
What could be causing this issue?