Question: I would like to call an API only the first time and then return the variable saved into service. 
I changed my code so:

```
export class CategoryService {
  categories: Category[]
  constructor(private http: HttpClient, private handleError: HandleErrorService) {
  }

  /************ REST CALLS *****************/
  /**
   * Retrieve configuration categories 
   */
  private getListCategories() {
    return this.http.get<Category[]>("/api/configuration/category").pipe(
      map(res => {
        this.categories = res;
        return res;
      }),
      catchError(err => this.handleError.handleError(err, { severity: 'error', summary: 'Error retrieving the list of categories', life: 5000 })) // then handle the error;
    );
  }
  /************ CATEGORY MANAGEMENT ************/
  get getCategories() {
    if (this.categories)
      return this.categories;
    else
      this.getListCategories().subscribe((categories:Category[])=>{
        return categories;
      });
  }

}

```

but in my `getCategories` I have to wait the response of API call. There is a pattern or best practice to achieve my target? Sure I can set the service variable from component but after that other component may call the API instead of the variable.

Answer: 
I tend to use an Observable for this to keep the subscribe function outside of the service for this type of thing.

```
getCategories() {
  return this.categories
    ? of(this.categories)
    : this.getListCategories().do((categories:Category[]) => {
        this.categories = categories;
      });
}

```
Try to use function localStorage to save your value and after recover it until you don't close your browser window.

```js
localStorage.setItem("key","value")
//after you can get the value using 
localStorage.getItem("key")
```

Don't forget that getItem will always return a String and setItem need a String as value and key