角5 httpClient和承诺

ped*_*nez 1 javascript typescript angular

我一直在看Angular 5的GET POST等:

get() {
    return this.httpClient.get<any>('https://api.github.com/users/seeschweiler');
}
Run Code Online (Sandbox Code Playgroud)

要么

http.get<ItemsResponse>('/api/items')
    .subscribe(
       // Successful responses call the first callback.
       data => {...},
       // Errors will call this callback instead:
       err => {
         console.log('Something went wrong!');
       });
Run Code Online (Sandbox Code Playgroud)

我不认为通常会使用诺言。

是因为不是真的需要它还是其他原因?

Sur*_*yan 6

默认情况下,Angular使用Observables。可观察对象使您可以更灵活地使用流。

如果要使用Promise,您仍可以使用toPromise函数将Observable转换为Promise 。