* example(dart/toh-4,5): getHeroesSlowly() to return getHeroes() Have `getHeroesSlowly()` delay and then return the value of `getHeroes()`. This makes it easier for user’s performing the tutorial to keep this slower method as they evolve toh-5 into toh-6. * example(ts/toh-4,5): getHeroesSlowly() to return getHeroes() Have `getHeroesSlowly()` delay and then return the value of `getHeroes()`. This makes it easier for user’s performing the tutorial to keep this slower method as they evolve toh-5 into toh-6.
		
			
				
	
	
		
			26 lines
		
	
	
		
			628 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			26 lines
		
	
	
		
			628 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| // #docplaster
 | |
| // #docregion
 | |
| import { Hero } from './hero';
 | |
| import { HEROES } from './mock-heroes';
 | |
| import { Injectable } from '@angular/core';
 | |
| 
 | |
| @Injectable()
 | |
| export class HeroService {
 | |
|   getHeroes(): Promise<Hero[]> {
 | |
|     return Promise.resolve(HEROES);
 | |
|   }
 | |
| 
 | |
|   getHeroesSlowly(): Promise<Hero[]> {
 | |
|     return new Promise<Hero[]>(resolve =>
 | |
|       setTimeout(resolve, 2000)) // delay 2 seconds
 | |
|       .then(() => this.getHeroes());
 | |
|   }
 | |
| 
 | |
|   // #docregion getHero
 | |
|   getHero(id: number): Promise<Hero> {
 | |
|     return this.getHeroes()
 | |
|                .then(heroes => heroes.find(hero => hero.id === id));
 | |
|   }
 | |
|   // #enddocregion getHero
 | |
| }
 |