简体   繁体   English

可以从rxjs中观察到

[英]Observable from rxjs

According to this topic: What is the difference between Promises and Observables? 根据这个主题: Promises和Observables有什么区别?

It was said there by many users that Observable is like a stream. 据许多用户说,Observable就像一个流。 It is really hard to me to understand, how particular endpoint for example this one: 我真的很难理解,例如这个特定端点如何:

https://jsonplaceholder.typicode.com/posts

can returns many values over time. 可以随时间返回许多值。 Since it is http request a single resposne is returned no matter if it was succeed or failed. 由于它是http请求,因此无论是成功还是失败,都会返回单个resposne。

You are correct, an http request will still only return a single event when using it with observables. 你是对的,http请求在与observable一起使用时仍然只会返回一个事件。 So if the only asyncronous operation you are doing is a single http request, there is no benefit from using an observable over a promise. 因此,如果您正在执行的唯一异步操作是单个http请求,那么使用可观察的承诺就没有任何好处。

The power of RxJS and observables comes when you start combining several observable streams together. 当您开始将多个可观察流组合在一起时,RxJS和observable的功能就出现了。 This can be done with operators like mergeMap, switchMap, forkJoin, combineLatest, etc. When you start doing this you can get a lot of benefits from having your http requests as observables. 这可以通过mergeMap,switchMap,forkJoin,combineLatest等运算符完成。当你开始这样做时,你可以通过将http请求作为可观察对象获得很多好处。

Take polling for example, creating an event stream that fires every 10 seconds and then polls the server with an http request is a use case where doing http requests with observables really shines. 以轮询为例,创建一个每10秒触发一次的事件流,然后使用http请求轮询服务器,这是一个使用observable进行http请求的用例。

Observables are useful when we want to manage and/or combine multiple events over time. 当我们想要随时间管理和/或组合多个事件时,Observable非常有用。

Lets imagine we have a method will return a promise for that JSON: 让我们假设我们有一个方法将返回该JSON的承诺:

const dataPromise = fetchData('https://jsonplaceholder.typicode.com/posts');

Also, imagine that we're tracking user presence on the current page: 另外,假设我们正在跟踪当前页面上的用户状态:

const userLeavePromise = trackUserPresence();

Lets now draw timelines for the data fetching and user leaving events. 现在让我们绘制数据提取和用户离开事件的时间表。 Here o stands for event, happening in time: 这里o代表事件,及时发生:

            1s  2s  3s  4s  5s
data        ------------o
userLeave   --------o

For example sake, data would be fetched at 4th second, while our restless user would leave the page at 3rd. 例如,数据将在第4秒获取,而我们不安分的用户将在第3页离开页面。 In this case we want to cancel the fetch operation and all post-processing callbacks (in Promise terms, all .then -ned functions). 在这种情况下,我们要取消读取操作和所有的后处理的回调(在承诺方面,所有的.then -ned功能)。

For such (and many more) situations RxJS provides lots of operators that let us combine events over time. 对于这种(以及更多)情况,RxJS提供了许多运算符,让我们可以随时间组合事件。

In this particular case we would take data, Until user leaves the page: 在这种特殊情况下,我们将获取数据, 直到用户离开页面:

// Observable for data
const data$ = fetchData('https://jsonplaceholder.typicode.com/posts');

// Observable for user leave
const userLeave$ = trackUserPresence();

data$
  .takeUntil(userLeave$)
  .subscribe(data => console.log(data))

Comparing http request by a promise and observable is not much different because the observable completes once the request is done so it is not a long lived observable. 通过promise和observable比较http请求没有太大的不同,因为一旦请求完成,observable就会完成,因此它不是一个长期可观察的。 But the one below emits each time you click the button. 但是每次单击按钮时都会发出以下一个。

 const { fromEvent } = rxjs; fromEvent(document.getElementById('clickMe'), 'click').subscribe(() => { console.log('clicked button'); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.4.0/rxjs.umd.min.js"></script> <button id="clickMe">Click me</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM