Angular 5,Observable 和异步函数 [英] Angular 5, Observable and async function
问题描述
我一直在尝试创建一个类似于 http.get(...) 但没有做任何 http 的函数.基本上我所做的是我定义了一个函数
I have been trying to create a function that is similar to http.get(...) but not doing any http. Basically what I have done is that I defined a function
async myFunc(): Observable<string> {
var myObservable: Observable<string>;
//.....
// the rest of the function's code comes here
// ...
return myObservable;
}
但是我总是收到一个错误,说它与 ES5/ES3 不兼容,应该返回一个 Promise 对象?但我想知道 Angular 5 的 http.get 函数是如何实现的?显然它返回一个 Observable.
But I always get an error that it is not compatible with ES5/ES3 and should return a Promise object? But I am wondering how it became possible with Angular 5's http.get function? Where clearly it returns an Observable.
谢谢,
阿塔尼斯
Thanks,
Artanis
推荐答案
异步/等待功能与承诺一起工作.您可以使用 Observable.defer
来定义具有异步/等待功能的 Observable.
The async/await functionality works with promises. You can use Observable.defer
to define an Observable with async/await functionality.
查看此帖子了解更多信息:https://medium.com/@benlesh/rxjs-observable-interop-with-promises-and-async-await-bebb05306875
See this post for more information: https://medium.com/@benlesh/rxjs-observable-interop-with-promises-and-async-await-bebb05306875
这篇关于Angular 5,Observable 和异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!