While I am familiar with using callbacks in JavaScript, I have recently started learning Angular and TypeScript. I am facing an issue with getting my data from a service back to where I need it after executing the callback function. The callback itself is functioning correctly as indicated by the console log statements being output in the correct order. However, I am struggling with the proper format to retrieve and handle this data. Despite searching for a solution online, I have not been able to find the right combination of code. I am currently sending my data back using `CallBack(data)`, but I am uncertain about how to receive it.
//Output
running
Got some data etc
Finished
// In Component
constructor(private imageService: ImageService) {
console.log('running');
this.imageService.test(()=>{
console.log('Finished');
});
}
// In a service
test(CallBack: { (callback: any): void; (): void; }) {
this.httpClient.post('http://localhost/api/gallery/buildGallery', this.postData)
.subscribe((data)=>{
console.log('Got some data',data);
CallBack(data);
},(error)=>{
console.log('error');
CallBack ();
});
}