码迷,mamicode.com
首页 > Web开发 > 详细

[RxJS] Reactive Programming - What is RxJS?

时间:2016-03-07 20:42:29      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

First thing need to understand is, Reactive programming is dealing with the event stream.

 

Event streams happens overtime, which not stay in the memory.

 

For example, the array we have:

var source = [‘1‘, ‘1‘, ‘foo‘, ‘2‘, ‘3‘, ‘5‘, ‘bar‘, ‘8‘, ‘13‘];

Which is stay in the momery. 

 

But the stream we have:

let logic = Rx.Observable.interval(400).take(9)
  .map( (i) => {
    let val = [‘1‘, ‘1‘, ‘foo‘, ‘2‘, ‘3‘, ‘5‘, ‘bar‘, ‘8‘, ‘13‘][i]
    return parseInt(val, 10);
})

Which happens overtime, every 400ms it return an Interge if possible.

 

So the main difference between array stay in memory and the events streams is array already stay in memory and the streams happens overtime.

 

But the nice things about the stream is we can still use the methods we have for array:

let logic = Rx.Observable.interval(400).take(9)
  .map( (i) => {
    let val = [‘1‘, ‘1‘, ‘foo‘, ‘2‘, ‘3‘, ‘5‘, ‘bar‘, ‘8‘, ‘13‘][i]
    return parseInt(val, 10);
})
  .filter( (number) => {
    return !isNaN(number)
  })
  .reduce( (acc, y) => {
     return acc + y;
  } );

let effect = logic.subscribe( (number) => {
  console.log(number);
});

 

[RxJS] Reactive Programming - What is RxJS?

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5251686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!