Cacoon
Cacoon

Reputation: 2538

Converting Promise information to observable

I followed the Tour of Hero guide but have decided Observables are better than promises but I am having trouble implementing them.

Here is my recipe service:

import { of } from 'rxjs/Observable/of';
...
  getRecipes(): Observable<Recipe[]> {
    return of(RECIPES);
  }

  getRecipe(id: number): Observable<Recipe> {
    return this.getRecipes()
      .subscribe(recipes => recipes.find(recipe => recipe.ID === id));
  }

I am not sure how to get a specific observable item from an obvserable array like I did with promises on the tutorial.

Upvotes: 0

Views: 55

Answers (1)

Deepak Sharma
Deepak Sharma

Reputation: 1901

You should use map for this case, your implementation can be following

 getRecipe(id: number): Observable<Recipe> {
     return this.getRecipes()
          .map(recipies => recipies.find(x => x.id === id));
  }

Upvotes: 1

Related Questions