wait for asynchronous functions to finish in Angular My component "ship-list" wants to get the list from the backend server. So, I made a Service (config.service.ts)
import { Injectable, OnInit } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class ConfigService {
url = 'http://192.168.1.26:8080/';
constructor(private http:HttpClient) { }
async getShipList() {
this.url = this.url + 'ships';
this.http
.get<any[]>(this.url)
.subscribe(
(response) => {
console.log("Response : ", response);
return (response);
},
(error) => {
console.log("Error ! : " + error);
}
);
}
}
Then, in my component, in the ngOnInit, i call for this function and wait for the results :
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { faCartArrowDown } from '@fortawesome/free-solid-svg-icons';
import { faChartArea } from '@fortawesome/free-solid-svg-icons';
// Services :
import { ConfigService } from '../config/config.service';
@Component({
selector: 'app-ship-list',
templateUrl: './ship-list.component.html',
styleUrls: ['./ship-list.component.scss']
})
export class ShipListComponent implements OnInit {
// Icons
faChartArea = faChartArea;
faCartArrowDown = faCartArrowDown;
// Var
searchText: any;
shipList: any;
url = this.configService.url;
constructor(private http:HttpClient, private configService: ConfigService) { }
async ngOnInit() {
this.shipList = await this.configService.getShipList();
console.log(this.shipList);
}
}
The problem is, my component doesn't really wait for my service to get the data. I can't find out what I did wrong.
Thank you in advance for your help !