admin管理员组文章数量:1317565
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 user8770372user87703722 Answers
Reset to default 6Angular 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
本文标签: javascriptangular 5 httpClient and PromisesStack Overflow
版权声明:本文标题:javascript - angular 5 httpClient and Promises - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742016906a2413941.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论