I would like to have the value of an observable at given instant, even if this one didn’t had any value, getting undefined/error is acceptable. But my code is currently waiting the observable to emit a value (logic !)
I have a service, and can’t modify it :
export class MyService {
private readonly isEnable$ = new ReplaySubject<boolean>(1)
public init(isEnable: boolean) {
this.isEnable$.next(isEnable);
}
public getFoo() {
return this.isEnable$.pipe(
map((isEnable: booelan) => isEnable ? foo : bar)
)
}
}
In my code i’m using this service to retrieve informations. And I’m on a case where i can’t initialise isEnable$.
So when I’m doing this, nothing happend, and this is normal.
this.myservice.getFoo().pipe(
tap((value) => console.log(value))
).subscribe();
I would like to have and error or undefined, or empty string, without waiting isEnable$ to emit.
So my workarround is :
race(
this.myservice.getFoo(),
of('').pipe(delay(1)),
).pipe(
tap((value) => console.log(value)) //got '' when not initialized, and got corrrect value when it is.
).subscribe()
First, can someone exaplain me why my first observable complete faster than 1ms ? It will always be true ?
Is there a better way to achieve this ?
Thanks !