let
연산자(operator) 정의: let(function): Observable
Let me have the whole observable.
Examples
Example 1: Reusing error handling logic with let
( jsBin | jsFiddle )
const retryThreeTimes = obs => obs.retry(3).catch(_ => Rx.Observable.of('ERROR!'));
const examplePromise = val => new Promise(resolve => resolve(`Complete: ${val}`));
const subscribe = Rx.Observable
.of('some_url')
.mergeMap(url => examplePromise(url))
.let(retryThreeTimes)
.subscribe(result => console.log(result));
const customizableRetry = retryTimes => obs => obs.retry(retryTimes).catch(_ => Rx.Observable.of('ERROR!'));
const secondSubscribe = Rx.Observable
.of('some_url')
.mergeMap(url => examplePromise(url))
.let(customizableRetry(3))
.subscribe(result => console.log(result));
Example 2: Applying map with let
( jsBin | jsFiddle )
const source = Rx.Observable.from([1,2,3,4,5]);
const test = source
.map(val => val + 1)
.subscribe(val => console.log('VALUE FROM ARRAY: ', val));
const subscribe = source
.map(val => val + 1)
.let(obs => obs.map(val => val + 2))
.subscribe(val => console.log('VALUE FROM ARRAY WITH let: ', val));
Example 3: Applying multiple operators with let
( jsBin | jsFiddle )
const source = Rx.Observable.from([1,2,3,4,5]);
const subscribeTwo = source
.map(val => val + 1)
.let(obs => obs
.map(val => val + 2)
.filter(val => val % 2 === 0)
)
.subscribe(val => console.log('let WITH MULTIPLE OPERATORS: ', val));
Example 4: Applying operators through function
( jsBin | jsFiddle )
const source = Rx.Observable.from([1,2,3,4,5]);
const obsArrayPlusYourOperators = (yourAppliedOperators) => {
return source
.map(val => val + 1)
.let(yourAppliedOperators)
};
const addTenThenTwenty = obs => obs.map(val => val + 10).map(val => val + 20);
const subscribe = obsArrayPlusYourOperators(addTenThenTwenty)
.subscribe(val => console.log('let FROM FUNCTION:', val));
Additional Resources
- let
- Official docs
Source Code: https://github.com/ReactiveX/rxjs/blob/master/src/operator/let.ts