Skip to content

Instantly share code, notes, and snippets.

@benlesh
Created August 23, 2017 01:02
Show Gist options
  • Save benlesh/3593b3af2d9b7246e99e14a3b5e1dca6 to your computer and use it in GitHub Desktop.
Save benlesh/3593b3af2d9b7246e99e14a3b5e1dca6 to your computer and use it in GitHub Desktop.
// 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