Skip to content

Instantly share code, notes, and snippets.

@kosich
Created May 8, 2019 21:59
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/421726c6d7dc3d426b6e7a7b58b1bd80 to your computer and use it in GitHub Desktop.
Save kosich/421726c6d7dc3d426b6e7a7b58b1bd80 to your computer and use it in GitHub Desktop.
startWith vs defaultIfEmpty with combineLatest operator
const { rxObserver } = require('api/v0.3');
const { timer, combineLatest } = require('rxjs');
const { startWith, filter, defaultIfEmpty, skip } = require('rxjs/operators');
// our value to compare to
// toString is needed only for vizualisation
const NO_VALUE = { toString: _=>'-' };
const a$ = timer(5).pipe(
// skip(1) // <-- uncomment this
);
const b$ = timer(10).pipe(
// skip(1) // <-- uncomment this
);
// vizualise a$ and b$
a$.subscribe(rxObserver('a$'));
b$.subscribe(rxObserver('b$'));
// startWith
combineLatest(
a$.pipe( startWith(NO_VALUE) ),
b$.pipe( startWith(NO_VALUE) )
)
.pipe(filter(([a, b]) => a !== NO_VALUE || b !== NO_VALUE))
.subscribe(rxObserver('result for startWith'));
// startWith
combineLatest(
a$.pipe( defaultIfEmpty(NO_VALUE) ),
b$.pipe( defaultIfEmpty(NO_VALUE) )
)
.pipe(filter(([a, b]) => a !== NO_VALUE || b !== NO_VALUE))
.subscribe(rxObserver('result for defaultIfEmpty'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment