partition
연산자(operator) 정의: partition(predicate: function: boolean, thisArg: any): [Observable, Observable]
Split one observable into two based on provided predicate.
Examples
Example 1: Split even and odd numbers
( jsBin | jsFiddle )
const source = Rx.Observable.from([1,2,3,4,5,6]);
const [evens, odds] = source.partition(val => val % 2 === 0);
const subscribe = Rx.Observable.merge(
evens
.map(val => `Even: ${val}`),
odds
.map(val => `Odd: ${val}`)
).subscribe(val => console.log(val));
Example 2: Split success and errors
( jsBin | jsFiddle )
const source = Rx.Observable.from([1,2,3,4,5,6]);
const example = source
.map(val => {
if(val > 3){
throw `${val} greater than 3!`
}
return {success: val};
})
.catch(val => Rx.Observable.of({error: val}));
const [success, error] = example.partition(res => res.success)
const subscribe = Rx.Observable.merge(
success.map(val => `Success! ${val.success}`),
error.map(val => `Error! ${val.error}`)
).subscribe(val => console.log(val));
Additional Resources
Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/partition.ts