FLYNNLABAboutMeCodingActivityStudy 2024초등수학링크오프너릴리즈노트WordTest
series and parallel by RxJS
2017-08-31

RxJS로 병렬 실행을 하기 위해서는 forkJoin을 사용하면 된다. 반대로 직렬 실행은 from / concatAll / bufferCount를 활용한다.

ts code

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/delay';
import 'rxjs/add/observable/forkJoin';
import 'rxjs/add/observable/from';
import 'rxjs/add/operator/concatAll';
import 'rxjs/add/operator/bufferCount';

const a = Observable.of(1).delay(500);
const b = Observable.of(2).delay(200);
const jobs = [a, b];
console.time('forkJoin');
Observable.forkJoin(jobs).subscribe(result => {
  console.log(result);
  console.timeEnd('forkJoin');
});

console.time('concatAll');
Observable.from(jobs).concatAll().bufferCount(jobs.length).subscribe(result => {
  console.log(result);
  console.timeEnd('concatAll');
});

result

[ 1, 2 ]
forkJoin: 509.567ms
[ 1, 2 ]
concatAll: 713.090ms