How to make outer Observable aware from inner Observable error or success in nested Observables?

How to make outer Observable aware from inner Observable error or success in nested Observables ?

Why onNext and onCompleted is undefined inside inner Observable ?

public updateDocument(item: Document): Observable<any> {
    this.firstUseOfflineContainer();
    let afiEdit = this.offlineData.afi.edit;

    //outer observable
    return Observable.create(observer => {
        //inner observable
        this.dataService.updateRequest(item).subscribe(
            (next) => {
                console.log("ok");
            },
            (err) => {                
                afiEdit.headers.push(item);
                //how to throw error to outer observable
            },
            () => {
                observer.onNext(item);
                observer.onCompleted();
            }
        );

        return () => console.log('cleanup message')
    });
}

My understanding is that your goal is simply to throw an error in the outer observable, when the inner observable fails.

So the code will look like this:

//Define the inner observable (probably an http call in your case)
//I mocked your call failing or passing with a random 50/50 chance
var innerObservable = new Rx.Observable(observer => {
  var didMockCallFail = Math.random() < .5; 
  if(didMockCallFail){
    console.log('Inner observable call failed');
    observer.error(new Error('Call failed!'));
  } else {
    console.log('Inner observable call returned data');
    observer.next({lolData: 'I am data'}); 
  }
})

//define the outer observable which just calls the inner observable
var outerObservable = new Rx.Observable(observer => {
  innerObservable.subscribe(
    data => observer.next(data),
    err => observer.error(err)
  )
});

outerObservable.subscribe(
  next => console.log('Got data!'),
  err => console.error('lol fail')
);

Furthermore, note that if you want to pass variables to Observables and make that code re-usable / avoid global scope, you can use a generator function, e.g.

function makeObs(someParam){
  return new Rx.Observable(observer => {
    observer.next(someParam);
  })
}

Let me know if you have any questions.

 

 

geT the cheap price in the ebay.com  walmart.com  godaddy.com