Whenever a button is clicked within my application, I have a requirement to upload specific items to the API before proceeding with the next functionality.
I've experimented with various approaches to ensure that the code waits for execution, but it seems to be advancing without completing the necessary tasks.
Below is an excerpt of the code snippet:
// Triggered on button click
onSendEmailClick(): void {
if (this.selectedEmail.Content) {
let attachments = this.assignEmailContent();
this.fireAfterCompleted(attachments);
}
}
// Executed upon button click
assignEmailContent() {
return this.assignEmailAttachments();
}
// It's crucial for attachmentArray to be fully populated before being returned
async assignEmailAttachments() {
let attachmentArray: any[] = this.selectedAttachments.map(async (item) => {
const response = await this.subscribeToContainerItem(item);
attachmentArray.push(response);
});
return attachmentArray;
}
// Invokes an API method to fetch item
async subscribeToContainerItem(item: any) {
this._azureStorageService.getContainerItemByContainerIdItemName(this._routeIdService.getStorageContainerId(), item.Name).subscribe(async (res: any) => {
return ({ filename: item.Name, path: res.url });
}, err => {
return (err);
});
}
// Handles API Request
getContainerItemByContainerIdItemName(containerId: string, itemName: string) {
let httpOptions = this.prepareOptions();
return this._http.get(environment.API + containerId + '/' + itemName, httpOptions);
}
The method assignEmailAttachments needs to await the response before moving forward. Any guidance on this issue would be greatly appreciated.