For some reason, my external API call is only working like 80% of the time, so if it fails, I'd like to at least try calling it 2-3 more times before giving an error. Is that possible?
Below is some code from my component and service files. The error I'm throwing is in my component file with the getCars() function. The API I'm calling is hosted on Heroku.
Component
import { Component, OnInit } from '@angular/core';
import { CarsService, Car } from '../cars.service';
@Component({
selector: 'app-car',
templateUrl: './car.component.html',
styleUrls: ['./car.component.css']
})
export class CarComponent implements OnInit {
cars: Car[];
constructor(
public carService: CarsService
) {
this.getCars();
}
getCars(){
this.carService.getCars().subscribe(
data => {
this.cars = data;
},
error => {
alert("Could not retrieve a list of cars");
}
)
};
Service
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { environment } from '../environments/environment';
export interface Car {
make: string;
model: string;
year: string;
}
@Injectable({
providedIn: 'root'
})
export class CarsService {
baseUrl = environment.baseUrl;
constructor(
public http: HttpClient
) { }
getCars() {
let url = this.baseUrl + '/api/car'
return this.http.get<Car[]>(url);
}
}