Skip to content

Instantly share code, notes, and snippets.

@ganqqwerty
Last active September 25, 2019 15:20
Show Gist options
  • Save ganqqwerty/797898f33899fd6fd49e2708e2ee82a2 to your computer and use it in GitHub Desktop.
Save ganqqwerty/797898f33899fd6fd49e2708e2ee82a2 to your computer and use it in GitHub Desktop.
export class ProductService {
private readonly URL = 'https://world.openfoodfacts.org/api/v0/product/';
constructor(private http: HttpClient) {
}
cache = {};
resolveProduct(code: string): Observable<any> {
if (this.cache[code]) {
console.log('Returning cached value!')
return of(this.cache[code])
}
console.log('Do the request again')
this.http.get(this.URL+code+'.json').pipe(
tap(resolvedValue => {
this.cache[code] = resolvedValue;
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment