最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - angular 5 httpClient and Promises - Stack Overflow

programmeradmin3浏览0评论

I've been looking at Angular 5's GET POST etc:

get() {
    return this.httpClient.get<any>('');
}

or

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!');
       });

I don't see that promises are usually used with it.

Is this because it's not really needed or some other reason?

I've been looking at Angular 5's GET POST etc:

get() {
    return this.httpClient.get<any>('https://api.github./users/seeschweiler');
}

or

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!');
       });

I don't see that promises are usually used with it.

Is this because it's not really needed or some other reason?

Share Improve this question edited Apr 17, 2019 at 6:43 Suren Srapyan 68.7k14 gold badges125 silver badges117 bronze badges asked Dec 11, 2017 at 17:02 user8770372user8770372
Add a ment  | 

2 Answers 2

Reset to default 6

Angular by defaults uses Observables. Observables give you more flexibility working with streams.

If you want to work with Promises you can still cast Observable into Promises by using toPromise function.

February 2024

toPromise() function call is now deprecated and we should be using firstValueFrom() or lastValueFrom() like so:

import { lastValueFrom } from 'rxjs';

return lastValueFrom(this.httpClient.get<any>(url));

returns a promise

发布评论

评论列表(0)

  1. 暂无评论