Article
home/blog/Loading

Create an Observable using RxJS

Creating an Observable in different ways is sometimes required based on the situation at hand. This is all you need to know!

Create a completed Observable

const someObject: string = 'some text';
return Observable.of(someObject);

Create a non-completed Observable

const someObject: string = 'some text';
return Observable.create((observer: Observer<string>) => {
    observer.next(someObject)
);

Create an eventually completing Observable

return Observable.create((observer: Observer<string>) => {
    observer.next('derp');
    setTimeout(() => {
        observer.next('completed now');
        observer.complete();
    }, 100);
);