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

119
Views
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 answers
Answer question

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 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!