问题
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