可观察和异步功能

可观察和异步功能

本文介绍了Angular 5,可观察和异步功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建类似于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.



谢谢,
阿塔尼斯(Artanis)



Thanks,
Artanis

推荐答案

async/await功能可与promises一起使用.您可以使用 Observable.defer 定义具有async/await功能的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,可观察和异步功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:25