RxJS: Setting default value for observable from another observable

coquin

I'm trying to create observable stream which takes user id from cookie and, if not found in cookie, fetches it from API. How can I do it 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 for user id
// Will be a 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 a defaultIfEmpty method which works with simple values only, not with observables. In Bacon.js there's or method for streams, which works perfectly fine, but I don't see anything similar in RxJS. Do I miss something or do I need to implement a custom observer?

Can Nguyen

You may concat the 2 observables and get the first emitted value:

var userIdStream = Rx.Observable.concat(cookieUserIdStream, userIdRequest).first();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rxjs: shorthand of create observable from value or observable

RxJs get value from observable

RxJs Observable - Handle 404 by emitting default value

RxJS Error Observable when another emits value

Transform data from one observable using another observable in angular rxjs

RxJs Observable in Observable from Array

Problem returning value from RxJS Observable

Difference in importing Observable from 'rxjs/Observable' and 'rxjs'

rxjs forkJoin nested observable inside another observable

How to create an Observable that depends on another Observable in RxJS

Rxjs: Create Observable that is a delayed offset of another observable

RxJS wait for observable then create another observable, and so on

How to set default form Value from Observable?

Invoking observable from another observable

Momentarily ignore values from Observable when another Observable provides a value

How to manipulate list of item from an Observable with value of another Observable

rxjs: how to return the result from another observable from catchError

RxJS, Observable, how to preserve value and switch map to another one

Rxjs One Observable Feeding into Another

another rxjs Observable retry strategy

RxJS Observable result from an Observable's output

Returning a Property from Observable as Observable in Rxjs Angular

Rxjs: how to switch from one observable to another mid stream

Using MergeMap with the array of data received from another observable - RxJs Angular

Rxjs combinelatest observable value is undefined

Observable that behaves as BehaviorSubject(observable with default value)

Rxjs: map each element in an array field from an observable of object with another observable

how to change the data of the first observable using data from another observable including the condition in angular/rxjs

How can i get value from an Rxjs observable in my code?