Last active
September 12, 2020 18:23
-
-
Save zachgoll/dd6714181439b9bd1b310e2db08772f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { interval, Observable } = Rx; | |
const { map, take, switchMap, startWith } = RxOperators; | |
// Emit value 1 at 1 second | |
// Emit value 3 at 4 seconds | |
// Emit value 5 at 5.5 seconds | |
// Complete at 5.5 seconds | |
function outerObservable() { | |
return new Observable(subscriber => { | |
setTimeout(() => { | |
subscriber.next(1); | |
}, 1000); | |
setTimeout(() => { | |
subscriber.next(3); | |
}, 4000); | |
setTimeout(() => { | |
subscriber.next(5); | |
subscriber.complete(); | |
}, 5500); | |
}) | |
} | |
// Emit the value 10 every second, starting at time 0, for 3 total seconds | |
function innerObservable() { | |
const valuesToEmit = [10, 10]; | |
return interval(1000).pipe( | |
take(valuesToEmit.length), | |
map(index => valuesToEmit[index]), | |
startWith(10) // emits 10 at time zero | |
) | |
} | |
outerObservable().pipe( | |
switchMap(outerObservableValue => { | |
return innerObservable().pipe( | |
map(innerObservableValue => { | |
return innerObservableValue * outerObservableValue; | |
}) | |
); | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment