RxJS: Setting default value for observable from another observable -


i'm trying create observable stream takes user id cookie and, if not found in cookie, fetches api. how can in rxjs?

var useridrequest = rx.observable.bindcallback(generateidasync); var cookieuseridstream = rx.observable.of(getcookievalue("user_id"))     .filter(x => x !== null);  var useridstream = cookieuseridstream.__ifemptythen__(useridrequest()); // <<< ???  // emulating async request user id // jsonp call in real app function generateidasync(cb) {     settimeout(() => {         cb(`id_${new date().gettime()}`);     }, 300); }  function getcookievalue(name) {     var regexp = new regexp(`${name}=([^;]*)`);     var match = document.cookie.match(regexp);      return match && match[1]; } 

there's defaultifempty method works simple values only, not observables. in bacon.js there's or method streams, works fine, don't see similar in rxjs. miss or need implement custom observer?

you may concat 2 observables , first emitted value:

var useridstream = rx.observable.concat(cookieuseridstream, useridrequest).first(); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -