[RxJS] Creation operator: create()
We have been using Observable.create() a lot in previous lessons, so let's take a closer look how does it work.
The create function:
var foo = Rx.Observable.create( function(observer){ observer.next(42); observer.next(100); observer.next(200); observer.complete(); }) ; foo.subscribe( (x)=>{console.log('next ' + x);}, (err)=>{console.log('err ' + err);}, ()=>{console.log('done');}, )
In deep, create() function equal to new Rx.Observable():
var foo = new Rx.Observable( function(observer){ observer.next(42); observer.next(100); observer.next(200); observer.complete(); }) ;
And this also equal to:
function subscribe(observer){ observer.next(42); observer.next(100); observer.next(200); observer.complete(); } var foo = new Rx.Observable( subscribe );
So, if we get rid of RxJS, then we can create the create() function like:
function subscribe(observer){ observer.next(42); observer.next(100); observer.next(200); observer.complete(); } var observer = { next: (x)=>{console.log('next ' + x);}, error: (err)=>{console.log('err ' + err);}, complete: ()=>{console.log('done');} } subscribe(observer);
Of course, it's useful to have the observable type because then it has all those nice operators that we saw and that we are also seeing new operators coming next. If you paid attention, then you're going to remember that in the subscribe, we had previously three functions here as argument. Instead of an object, as we have now, we had just these three functions.
Also, the observable type, it converts these three functions into an observer object. Before it calls this, it will actually take these three functions and put labels in front of them like that, to create the observer object. It's normalizing it.
相关文章
- 代码审计之create_function()函数
- 第一个 mac 程序 Create-JSON-Model
- [CSS3] Create Viewport Responsive Layout Spacing Using CSS Clamp
- [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
- [SCSS] Create a gradient with a Sass loop
- [RxJS] Create a Reusable Operator from Scratch in RxJS
- [Angular] Create a simple *ngFor
- [D3 + AngularJS] 15. Create a D3 Chart as an Angular Directive
- 未解决Unable to use slave's temporary directory /tmp - Can't create/write to file '/tmp/SQL_LOAD-' (Err
- [Bash] Create a Bash Script that Accepts Named Options with getopts
- [Docker] Create and run a Java application Docker image
- [Javascript] Broadcaster + Operator + Listener pattern -- 15. Create a Sequence of Broadcasters
- [React] Use Environment Variables with Create React App
- [Javascript] Create Your First Iterator in JavaScript
- [React] Create a Persistent Reference to a Value Using React useRef Hook
- [React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions
- [Python] Create Unique Unordered Collections in Python with Set
- [Angular] Create a custom validator for reactive forms in Angular
- Create new SAP DDL view and click finish in wizard
- 线程异常:undefined reference to 'pthread_create' 处理
- frida anti pthread_create