3 Common Mistakes I see people use in Rx and the Observable Pattern

Chris Pawlukiewicz
Apr 25, 2017 · 3 min read

1. Taking Data out of Observables to put into other Observables.

initialize() {
let id;
this.appParameters.subscribe(params => id = params['id']);
if (id !== null && id!== undefined) {
this.getUser(id).subscribe(user => this.user = user);
}
}

2. Using multiple Observable.subscribes in an expression.

initialize() {
this.appParameters.subscribe(params => {
const id = params['id'];
if (id !== null && id!== undefined) {
this.getUser(id).subscribe(user => this.user = user);
}
});
}
initialize() {
this.appParameters
.map(params => params['id'])
.switchMap(id => {
if(id !== null && id !== undefined) {
return this.getUser(id)
}
})
.subscribe(user => this.user = user);
}

3. Not returning an Observable when it’s expected.

initialize() {
this.appParameters
.map(params => params['id'])
.switchMap(id => {
if(id !== null && id !== undefined) {
return this.getUser(id)
} else {
return Observable.empty()
}
})
.subscribe(user => this.user = user);
}
initialize() {
this.appParameters.map(params => params['id'])
.switchMap(id => id ? this.getUser(id) : Observable.empty())
.subscribe(user => this.user = user);
}

Chris Pawlukiewicz

Written by

Novelist, technology freak, software developer, entreprenuer. Founder of kingdomworks — https://kingdomworks.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade