RxJS

RxJS可以用来异步修改数据,可以用在WebAPI请求时的延迟处理等地方

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
    import { Observable } from 'rxjs';

    const ober = new Observable(a => {
      a.next(1);
      a.next(2);
      a.next(3);
      setTimeout(() => {
        a.next(4);
        a.complete();
      });
    });

    console.log('start subscribe');
    ober.subscribe({
      next(x) { console.log('got value ' + x); },
      error(err) { console.error('something wrong occurred: ' + err); },
      complete() { console.log('done'); }
    });
    console.log('just after subscribe');

输出的结果是: RxJS Log

可以看到,Observable中的数值处理会在subscribe订阅的时候才会处理,setTimeout中的代码会再延迟一步处理