Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda