let

signature: let(function): Observable

Let me have the whole observable.

let - 图1

Examples

Example 1: Reusing error handling logic with let

( jsBin |
jsFiddle )

  1. // custom error handling logic
  2. const retryThreeTimes = obs =>
  3. obs.retry(3).catch(_ => Rx.Observable.of('ERROR!'));
  4. const examplePromise = val =>
  5. new Promise(resolve => resolve(`Complete: ${val}`));
  6. //faking request
  7. const subscribe = Rx.Observable.of('some_url')
  8. .mergeMap(url => examplePromise(url))
  9. // could reuse error handling logic in multiple places with let
  10. .let(retryThreeTimes)
  11. //output: Complete: some_url
  12. .subscribe(result => console.log(result));
  13. const customizableRetry = retryTimes => obs =>
  14. obs.retry(retryTimes).catch(_ => Rx.Observable.of('ERROR!'));
  15. //faking request
  16. const secondSubscribe = Rx.Observable.of('some_url')
  17. .mergeMap(url => examplePromise(url))
  18. // could reuse error handling logic in multiple places with let
  19. .let(customizableRetry(3))
  20. //output: Complete: some_url
  21. .subscribe(result => console.log(result));
Example 2: Applying map with let

( jsBin |
jsFiddle )

  1. //emit array as a sequence
  2. const source = Rx.Observable.from([1, 2, 3, 4, 5]);
  3. //demonstrating the difference between let and other operators
  4. const test = source
  5. .map(val => val + 1)
  6. /*
  7. this would fail, let behaves differently than most operators
  8. val in this case is an observable
  9. */
  10. //.let(val => val + 2)
  11. .subscribe(val => console.log('VALUE FROM ARRAY: ', val));
  12. const subscribe = source
  13. .map(val => val + 1)
  14. //'let' me have the entire observable
  15. .let(obs => obs.map(val => val + 2))
  16. //output: 4,5,6,7,8
  17. .subscribe(val => console.log('VALUE FROM ARRAY WITH let: ', val));
Example 3: Applying multiple operators with let

( jsBin |
jsFiddle )

  1. //emit array as a sequence
  2. const source = Rx.Observable.from([1, 2, 3, 4, 5]);
  3. //let provides flexibility to add multiple operators to source observable then return
  4. const subscribeTwo = source
  5. .map(val => val + 1)
  6. .let(obs =>
  7. obs
  8. .map(val => val + 2)
  9. //also, just return evens
  10. .filter(val => val % 2 === 0)
  11. )
  12. //output: 4,6,8
  13. .subscribe(val => console.log('let WITH MULTIPLE OPERATORS: ', val));
Example 4: Applying operators through function

( jsBin |
jsFiddle )

  1. //emit array as a sequence
  2. const source = Rx.Observable.from([1, 2, 3, 4, 5]);
  3. //pass in your own function to add operators to observable
  4. const obsArrayPlusYourOperators = yourAppliedOperators => {
  5. return source.map(val => val + 1).let(yourAppliedOperators);
  6. };
  7. const addTenThenTwenty = obs => obs.map(val => val + 10).map(val => val + 20);
  8. const subscribe = obsArrayPlusYourOperators(addTenThenTwenty)
  9. //output: 32, 33, 34, 35, 36
  10. .subscribe(val => console.log('let FROM FUNCTION:', val));

Additional Resources

  • let
    :newspaper: - Official docs

:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/let.ts