Skip to content

Instantly share code, notes, and snippets.

@kosich
Last active April 24, 2019 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosich/201babaf39591ca11c00d9224e78efd0 to your computer and use it in GitHub Desktop.
Save kosich/201babaf39591ca11c00d9224e78efd0 to your computer and use it in GitHub Desktop.
example of concatMap with a Promise
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