Rx.js, observable, Subject, Observer, Promise, eventemitter understanding __js

Source: Internet
Author: User
Tags emit event listener

First, rx.js understanding

Rx.js is a function-responsive programming, observable, SUBJECT, observer is the implementation of its external delivery.


Observable is actually promise++ (Promise object is used to perform an asynchronous operation). In the Rx, you can simply convert a Promise to a observable, just need: var stream = Rx.Observable.fromPromise (Promise), and then we'll use him. The only difference between observable and promise++ is that the former is incompatible with promise/a+, but in theory there is no conflict. Promise is actually a single value observable, but the latter is more than allowing multiple return values (multiple emit). The Observer interface provides developers with the means to listen asynchronously to the intersection of a target element and its ancestors or windows.


The difference between observer and subject: I understand that observer calls subscribers ' next, complete, and error to deliver the data, so every subscription observer executes the data to send it; subject own Next, Complete, the error function, so just the Subscriber is logged, whether to send data, when to send data by the subject to decide for themselves.


Second, promise understanding

The so-called promise is simply a container that holds the result of an event (usually an asynchronous operation) that will end in a future. Syntactically, Promise is an object that obtains the result of an asynchronous operation, obtains an asynchronous result, and executes the user action passed by the then method. Promise provides a unified API that can be handled in the same way with all kinds of asynchronous operations.


Iv. understanding of Eventemitter

Eventemitter, when you need to trigger an event, emit the event you want, and then in other code, listen to the event for processing.

Emit code

Const EV = Eventemitter ();

Ev.emit (' event ', data);


User code, add event Listener

Ev.on (' event ', data => process (data));

Eventemitter is especially useful when making a library or framework, because it decouple the handling of events and events, and the triggers of events do not have to worry about who is going to handle the event or how.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.