Implementing the async-await
technique to handle the promises
, I encountered an issue where then()
is being called before the completion of Promise.all()
.
Revised
After changing from
Promise<void>
toPromise<string>
, the problem persists withthen()
executing prematurely.
// This function uploads an image and returns its path
private async imageUpload(): Promise<string> {
try {
let img = await this.file.upload(this.imageToUpload, 'fileInput_image')
img.subscribe((path: string) => {
this.bg_img_url.setValue(path)
console.log(this.bg_img_url.value) // displays url
return this.bg_img_url.value
})
}
}
// This function uploads an icon and returns its path
private async iconUpload(): Promise<string> {
try {
let icon = await this.file.upload(this.iconToUpload, 'fileInput_icon')
icon.subscribe((path: string) => {
this.item_icon.setValue(path)
console.log(this.item_icon.value) // shows url
return this.item_icon.value
})
}
}
The issue arises when trying to access values at a later stage
Promise.all([this.iconUpload(), this.imageUpload()])
.then((x) => {
console.log(this.bg_img_url.value) // ''
console.log(this.item_icon.value) // ''
})
Is there a way to ensure that promise.all()
resolves before triggering then()
?
Appreciate all the time and effort dedicated to helping me with this problem. Thank you for your valuable input!