Created
August 23, 2017 01:02
-
-
Save benlesh/3593b3af2d9b7246e99e14a3b5e1dca6 to your computer and use it in GitHub Desktop.
Examples for https://medium.com/p/651f4b7d45cf/edit
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
// An observable that emits 10 multiples of 100 every 1 second | |
const source$ = Observable.interval(1000) | |
.take(10) | |
.map(x => x * 100); | |
/** | |
* returns a promise that waits `ms` milliseconds and emits "done" | |
*/ | |
function promiseDelay(ms) { | |
return new Promise(resolve => { | |
setTimeout(() => resolve('done'), ms); | |
}); | |
} | |
// using it in a switchMap | |
source$.switchMap(x => promiseDelay(x)) // works | |
.subscribe(x => console.log(x)); | |
source$.switchMap(promiseDelay) // just a little more terse | |
.subscribe(x => console.log(x)); | |
// or takeUntil | |
source$.takeUntil(doAsyncThing('hi')) // totally works | |
.subscribe(x => console.log(x)) | |
// or weird stuff you want to do like | |
Observable.of(promiseDelay(100), promiseDelay(10000)).mergeAll() | |
.subscribe(x => console.log(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment