I have the first function that looks like this:
private async checkIsExists(): Promise<Boolean> {
this.repositoryService.getEntry(this.id)
.subscribe({
error: (err) => {
return false;
}
});
return true;
This function should return false if any error occurs, such as a 404. In the repositoryService i have the getEntry function that looks like this:
getEntry(entryId: string) {
return this.collectionsApi.getEntry(entryId);
}
Which is not an async function My question would be, how could i make the check function work correctly, at this moment it returns true no matter what, because it doesnt wait for the data to be fetched, i would like to change only this function if possible
Update: I changed the function and the call to this:
private checkIfShareExists(): Observable<Boolean> {
return this.repositoryService.getEntry(this.id).pipe(
catchError(() => of(false)),
map( () => {
return true;
})
)}
...
this.checkIfShareExists().subscribe(exists => {
console.log(exists);
});
But it still prints true always, even though the error is thrown
Assuming getEntry is an observable (since you are subscribeing to it) you can use async await if you transform it into a promise:
private async checkIsExists(): Promise<Boolean> {
return await this.repositoryService.getEntry(this.id).toPromise().then(r => true).catch(r => false);
}
After this you can use that function inside another async-await block to get your boolean result:
async myFunc(){
var couldGet = await this.myComponent.checkIsExist();
if(!couldGet) { console.error("sadface.jpg") }
}