Angular 5, Observable and async function

不想你离开。 提交于 2021-02-08 10:37:48

问题


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;
}

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


回答1:


The async/await functionality works with promises. You can use Observable.defer to define an Observable with async/await functionality.

See this post for more information: https://medium.com/@benlesh/rxjs-observable-interop-with-promises-and-async-await-bebb05306875



来源:https://stackoverflow.com/questions/51680984/angular-5-observable-and-async-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!