0
votes

I know how to emit a value to the observer and subscribe to them using observable, as shown here

var observable = new Observable(observer => {
      observer.next(1);
      observer.next(2);
      observer.next(3);
    }).subscribe((success) => {
      console.log(success);
    })

but if I want to do the same thing with the function, ie. I have something like this, then how can I achieve it

 var observable = new Observable(observer => {
      observer.next(function () {
        setTimeout(() => {
          1
        }, 1000)
      })
      observer.next(function () {
        setTimeout(() => {
          2
        }, 1000)
      })
      observer.next(function () {
        setTimeout(() => {
          3
        }, 1000)
      })
    }).subscribe((success) => {
      console.log(success);
    })

is it possible, all I have to do is call a series of async functions, how can I do it

UPDATE

i want to call a series of asnc fuctions in a sequence, ie. the second should be called only after the completion of the first functions operation and so on and so forth

1
are you looking for automatic subscription getting fired - Aravind
You need to call the next inside the setTimeOut - Vinod Bhavnani
i want to call a series of aysnc fuction in a sequence, ie. the second should be called only after the completion of the first functions operation - Lijin Durairaj

1 Answers

1
votes

You can do something like this. This is just the fundamental here. You can call your async instead of emitting static values.

var ParentObservable = new Observable();

ParentObservable.subscribe((res) => {
  //res is your response from async calls
  //Call asyncCall again from here
})

function asyncCall(){
  this.http.get("your URL").map((res)=> res.json()).subscribe((res)=>{
      ParentObservable.next(res);
  })
}