I am currently working with Angular CLI version 8.3.2 and Node version 10.16.3 on win32 x64. My project involves integrating an Angular frontend with a .NET backend. The frontend communicates with the backend API to retrieve a list of messages using an HTTP GET request, and then displays these messages in the browser.
Here is the implementation of the getMessage
function in the WebService service component:
public async getMessage() {
await this.http.get<Message[]>(this.BASE_URL + '/Messages').subscribe(
result => {
if (result) {
this.updateCollection(result);
}
},
error => console.error(error)
);
}
Additionally, here is the MessagesComponent
that invokes the webService to retrieve the messages:
export class MessagesComponent {
constructor(private webService: WebService) {
console.log("messages has " + webService.messages.length + " entries");
webService.getMessage();
console.log("messages has " + webService.messages.length + " entries");
}
}
Although both functions execute without any errors, the issue lies in the delayed execution of the getMessages
function. Despite being called within the constructor of the MessagesComponent
, it appears to fetch the data only after the constructor, resulting in an empty message list upon rendering the page. I have attempted invoking getMessage
without using async/await
, however, the behavior persists. How can I ensure the messages are retrieved promptly without latency?