skip to content
Alvin Lucillo

Http client promise

/ 1 min read

💻 Tech

Per Angular team, it’s recommended to use signals than RxJS because the former is lightweight and its learning curve is not steep. The latter is recommended to be used for more advanced handling of reactive values. In this case, when using HttpClient, we can convert the Observable into a Promise by using firstValueFrom.

http = inject(HttpClient);

async loadAllUsers(): Promise<Users[]> {
  const users$ = this.http.get<GetUsersResponse>(
    `${this.url}/users`
  );
  const response = await firstValueFrom(users$);

  return response.users;
}