I am used to using async/await in Asp.net. I now need to make several API calls (4) within the ngBootstrap Modal Service. The user will click on a button to open a Modal. The first API call gets the base information that the Modal needs. The async OpenModal() method is decorated with async and the first API call uses async res as shown below.
this.service.findContent(id).subscribe(
async res => {
//other code
var socialDataResult = await this.getSocialData();
console.log('socialDataResult ', socialDataResult);
//other code
},
async getSocialData() {
let result: SocialData;
this.socialDataService.findSocialData().subscribe(
async res => {
let socialData = new SocialData(res);
console.log('socialData ', socialData);
result = socialData;
return result;
},
err => {
console.log(err);
}
)
return result;
}
I would expect that the code would wait for the response to return from getSocialData before it writes to the console. As-is, it writes the console log just below the calling method before it writes the console log in the method being called. The console log in the calling method says "undefined". The console log in the method being called shows the SocialData object with all the data.
It's also important to note that when I hover over await this.getSocialData();, I get this.
(method) ContentComponent.getSocialData(): Promise<SocialData>
Any help is much appreciated. Thanks!