Open damirarh opened 1 year ago
Imported comment written by Emiliano on 2017-08-04T15:10:58
How did you implement the toPromise() method?
Do you have the source code?
Thanks
Imported comment written by +380 on 2017-08-04T16:34:22
Add to main.ts:
import 'rxjs/add/operator/toPromise';
Imported comment written by Damir Arh on 2017-08-04T17:44:53
Observable.toPromise() is a part of RxJS. To use it, you only need to add the following import at the top of the source file:
import 'rxjs/add/operator/toPromise';
Imported comment written by Ken Perregaux on 2017-09-12T20:18:36
What happens if you service throws an error? What does the client do?
e.g. You want to display an error to the user.
Imported comment written by Damir Arh on 2017-09-13T12:49:59
In case of an error, service methods still reject the returned promises, as they did before my changes to the code.
When using async/await in component methods as well, you can catch the errors with a try/catch block as I did in the changed getHeroes method, for example:
try {
let heroes = await this.heroService.getHeroesSlowly();
this.heroes = heroes;
} catch (error) {
/* handle error */
}
Imported comment written by Ryan Buening on 2017-11-21T03:48:50
@damirarh:disqus I'm trying something similar to what you have, but TypeScript 2.6.1 is throwing a compile error. I created a StackOverflow question here: https://stackoverflow.com/q.... Any ideas?
Imported comment written by Damir Arh on 2017-11-21T07:21:38
It seems Typescript 2.6.1 is more strict regarding return type checking. Check Aaron's answer for the options that are available to you. I might update the blog post after I take a closer look at it myself.
Imported comment written by Ryan Buening on 2017-11-21T12:15:23
Thanks for the reply. I'll look into Aaron's answer.
Imported comment written by Damir Arh on 2017-12-26T09:45:31
Since I've written the blogpost, the Tour of Heroes tutorial has been rewritten to a great extent. HeroService functions don't even return promises any more, making the conversion to async/await as described above impossible and useless. In light of that, keeping the blogpost unchanged makes much more sense. Fortunately I included original function implementations in the post, preserving most of its educational value.
Imported
URL: https://www.damirscorner.com/blog/posts/20170127-Angular2TutorialWithAsyncAndAwait.html