Q:

Service returning data but can't subscribe

In my component.ts I am calling a function from my service class which retrieves data from my backend rest api. When logging the result I successfully retrieved the data but when trying to subscribe to it in my component nothing happens
vehicles: VehicleDto[] = [];

  constructor(private myService: MyService) {

    console.log(this.myService.getVehicles());
    console.log("Before Service call");
    this.myService.getVehicles().subscribe((result: any[]) => {

      console.log("service called");
      this.vehicles = result;
      console.log(this.vehicles); //this is console for vehicles     
    });
    console.log("after service call");
  }

In my service class:
 getVehicles() : Observable<VehicleDto[]> {

    let resultSubject: AsyncSubject<VehicleDto[]> = new AsyncSubject();
    let httpOptions = this.kecloakService.buildHeaderWithToken();
    this.configurationService.loadNetworkConfiguration(false).subscribe((config: NetworkConfiguration) => {

      if (config) {
        this.http.get(config.getHostUrl + this.url + "getVehicles", httpOptions)
        .subscribe((entities: VehicleDto[]) => {
          let results: VehicleDto[] = [];
          for(let entity of entities) {
            results.push(entity);
          }
          resultSubject.next(results);
          resultSubject.complete;
        });
      }
    });
    return resultSubject;
  }

This is the result I get in the console

As you can see I do get the array back, but inside the subscribe the result never gets logged or assigned to vehicles.

A:

You can create an Observable and should work : 
  getVehicles(): Observable<VehicleDto[]> {
    let httpOptions = this.kecloakService.buildHeaderWithToken();
    return new Observable((obs => {
      this.configurationService.loadNetworkConfiguration(false).subscribe((config: NetworkConfiguration) => {
        if (config) {
          this.http.get(config.getHostUrl + this.url + "getVehicles", httpOptions)
            .subscribe(
              (entities: VehicleDto[]) => {
                let results: VehicleDto[] = [];
                for (let entity of entities) {
                  results.push(entity);
                }
                obs.next(results);
                obs.complete();
              }
            );
        }
      });
    }));
  }

