I'm using rxjs
with typescript. I wrote following two simple functions. But I guess rxjs
already has such functionality. Does rxjs
?
export function isObservable(value: any): value is Observable<any> {
return value instanceof Observable;
}
export function createObservable<T>(value: T | Observable<T>): Observable < T > {
return isObservable(value) ? value : Observable.of(value);
}
I don't think RxJS 5 has exactly this functionality already because it's you who should be aware of what type of data you're working with.
However, you can make use of the fact that RxJS handles any Observables, Promises, Observable-like, array-like and so on ... objects the same way and write the following:
const val1 = Observable.of(42);
const val2 = [42];
Observable.from(val1).subscribe(console.log);
Observable.from(val2).subscribe(console.log);
This will in both cases print just 42
even though once I used an Observable with a single value and then an array with a single value.
See live demo: https://jsbin.com/fubobon/3/edit?js,console
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments