Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you return new Observable(function(observer) { ... with RxJS v5?

I am trying to replace all the promises that my functions return with Observables. From this post, I learned that I should no longer use "new Observable" Observable.forkJoin and array argument

What is the RxJS v5 syntax to achieve this as such to achieve asynchronous waiting?

thirdFunction() {

    let _self = this;

    return new Observable(function(observer) {

        ...

        observer.next( responseargs );
        observer.complete();
    });
}

Thank you greatly for help you can offer.

like image 343
Benjamin McFerren Avatar asked Mar 04 '16 16:03

Benjamin McFerren


People also ask

How do you make a function Observable RxJS?

Creating Observableslink The following example creates an Observable to emit the string 'hi' every second to a subscriber. import { Observable } from 'rxjs'; const observable = new Observable(function subscribe(subscriber) { const id = setInterval(() => { subscriber.next('hi'); }, 1000); });

How do I return an Observable subscription?

You can't return an observable from subscribe but if you use map instead of subscribe then an Observable is returned.

What does .subscribe return in Angular?

This is a JavaScript object that defines the handlers for the notifications you receive. The subscribe() call returns a Subscription object that has an unsubscribe() method, which you call to stop receiving notifications.

What does an Observable return?

An Observable is basically a function that can return a stream of values to an observer over time, this can either be synchronously or asynchronously. The data values returned can go from zero to an infinite range of values.


1 Answers

There are a set of methods to create observables for different use cases:

  • of - create an observable and directly trigger an event with the provided value
  • timeout - create an observable that triggers an event after an amount of time
  • interval - create an observable that triggers repeatly after an amount of time

This link provides a list of them by category:

  • https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/categories.md
like image 182
Thierry Templier Avatar answered Oct 11 '22 06:10

Thierry Templier