As a beginner in typescript and angular, I am trying to understand observables. My query is related to a method that fetches the favicon of a given URL. How can I modify this method to use observables instead of promises?
getFavIcon(url: string): Observable<any> {
return new Observable((observer) => {
const hostname = this.getDomain(url);
const src = 'http://' + hostname + '/favicon.ico';
const image = new Image();
image.onerror = () => {
observer.error({ hasFavourite: false });
};
image.onload = () => {
observer.next({ hasFavourite: true, imageURL: src });
observer.complete();
};
image.src = src;
});
}