Last active
September 25, 2019 15:20
-
-
Save ganqqwerty/797898f33899fd6fd49e2708e2ee82a2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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