This repository was archived by the owner on Dec 4, 2017. It is now read-only.
This repository was archived by the owner on Dec 4, 2017. It is now read-only.
Typing issue in Tour of Heroes example for chapter 5 #2443
Closed
Description
In chapter 5 "Services" of the Tour of Heroes tutorial, you can find this example:
getHeroesSlowly(): Promise<Hero[]> {
return new Promise<Hero[]>(resolve =>
setTimeout(resolve, 2000)) // delay 2 seconds
.then(() => this.getHeroes());
}
Not being familiar with Promises and TypeScript, this got me confused. How come the typing is done in the constructor with the <Hero[]> type argument? The first Promise constructed actually does not return this array, it is only the then
statement that is indeed typed. So I believe the following syntax would make more sense:
getHeroesSlowly(): Promise<Hero[]> {
return new Promise(resolve =>
setTimeout(resolve, 2000)) // delay 2 seconds
.then<Hero[]>(() => this.getHeroes());
}
Sorry, but I come from java where this could never get past the compiler !
Metadata
Metadata
Assignees
Labels
No labels