0

Is it possible to change the property of an emitted value from a Subject before passing it to another Observable? For example:

this.subscriptions.push(this.myService.mySubject.subscribe(value => {
    value.property = true;
    this.subscriptions.push(this.myOtherService.myMethod(value.propertyTwo).subscribe(value => {
    // do things with value
    });
}, (error) => {
    // how can I still access said value in the error handler?
});

I push my subscriptions to a private subscriptions array so that I can unsubscribe from them in my ngOnDestroy. I'd like to get to get rid of the nested subscription in this case and was wondering if it was possible to do so?

Also, how can I still access the value variable in the error handler of my subscription?

Thanks

1 Answer 1

1

Chain them up with switchMap and handle error with catch. In well written observable chains usually you don't need many subscriptions

this.subscriptions.push(this.myService.mySubject.switchMap(value => {
value.property = true;
return this.myOtherService.myMethod(value.propertyTwo)})
.catch(e=>Observable.of(e)).subscribe())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.