Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Await for a function to finish not working

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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") }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!