8

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.

Community
  • 1
  • 1
Benjamin McFerren
  • 822
  • 5
  • 21
  • 36
  • I guess he means the creation functions listed in http://reactivex.io/RxJS/ like `Observable.of(...)` – Günter Zöchbauer Mar 04 '16 at 16:15
  • specifically a function that returns an Observable. When I go to that link you posted and I click create, the following page is very confusing. Can you please offer a fiddle or plknr that demonstrates a function that returns an Observable with RxJs v5? In v4 , I had it working with new Observable but I do not know the equivalent with v5 – Benjamin McFerren Mar 04 '16 at 16:41

1 Answers1

11

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:

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • which one do I use if the caller simply needs to subscribe? Where in the function definition do I put the observer.next( ... ) and the observer.complete() ? My use case is that I want the caller to invoke my function and wait until that function completes so that, when it completes, the caller can then run a block of code found in the subscribe ( ... ) .. Basically I am trying to replicate returning $q as we did with Angular 1 – Benjamin McFerren Mar 04 '16 at 16:44
  • How does your function notify that its processing has ended? – Thierry Templier Mar 04 '16 at 16:56
  • currently, with RxJS v4 new Observable, it notifies that its processing has finished by calling observer.next( responseargs ); observer.complete(); But i have found this convention is not compatible with forkJoin so I am trying to find the RxJS v5 way to do it – Benjamin McFerren Mar 04 '16 at 17:05
  • What do you mean by "not compatible"? Here is a plunkr describing how to use a raw observable with `Observable.forkJoin`: https://plnkr.co/edit/XqHYTqL2BadyVltajVhs?p=preview. – Thierry Templier Mar 04 '16 at 17:25
  • 3
    thnx - what I needed was this: return Observable.create((observer) => { ... observer.next("test"); observer.complete(); – Benjamin McFerren Mar 04 '16 at 22:32
  • also, after some testing, I noticed that you have to return something in the next( ... ) arglist in order for the asynch to work – Benjamin McFerren Mar 04 '16 at 22:54