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

120
Visualizações
Is using async/await in promise request more efficient?

What is the best way to make requests?

I have an http request method in project.service.ts. I can make the request with the following structure:

getProjectById(projectId: number) {
  return this.http.get<Project>(`${this.url}/search/projects/${projectId}`).toPromise();
}

And call it with the following structure in project.component.ts:

async getProject(projectId: number) {
  this.isLoading = true;
  try {
    this.project = await this.api.projects.getProjectById(projectId);
  } finally {
    this.isLoading = true;
  }
}

But I can also make the request like this:

async getProjectById(projectId: number) {
  return await this.http.get<Project>(`${this.url}/search/projects/${projectId}`).toPromise();
}

Or this:

getProjectById(projectId: number): Observable<Project> {
  return this.http.get<Project>(`${this.url}/search/project/${projectId}`);
}

But I'd have to subscribe to this function.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This is rather question of design of code and clean code, than efficiency. I reckon following the guidelines of Angular built-in Http module.

You need to keep the code clean, and follow the RxJS flow, rather than convert it to async code.

So you need to use this:

getProjectById(projectId: number): Observable<Project> {
  return this.http.get<Project>(`${this.url}/search/project/${projectId}`);
}

If you have no reason, don't complicate it.

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