Last active
April 24, 2019 17:45
-
-
Save kosich/201babaf39591ca11c00d9224e78efd0 to your computer and use it in GitHub Desktop.
example of concatMap with a Promise
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 { rxObserver } = require('api/v0.3'); | |
const { timer } = require('rxjs'); | |
const { concatMap } = require('rxjs/operators'); | |
// our source$ will emit values at | |
// 0, 100, 200, 300... | |
const source$ = timer(0, 100); | |
const concatMap$ = source$.pipe( | |
concatMap(i => makeAPromise(i * 50)) | |
); | |
// visualization | |
source$.subscribe(rxObserver('source$')); | |
concatMap$.subscribe(rxObserver('concatMap( promise )')); | |
// helpers | |
function makeAPromise(timeout){ | |
// return a promise that will be resolved | |
// after a `timeout` | |
return new Promise(resolve=>{ | |
setTimeout(()=>resolve(Date.now()), timeout); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment