[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.

posted @   Zhentiw  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示