I have four subscribes that dependable on each other. I know there are too many answers regarding avoiding nested subscribe. But nobody answers more than two levels.
how to avoid that many nested subscribe
this is my component code
if (this.code) {
this.appService.updateData('Users/clever_token', {code: this.code}).subscribe(data => {
if (data.detail) {
this.accessToken = data.detail.access_token;
this.appService.getCleverData('/v2.1/me', this.accessToken).subscribe(data1 => {
if (data1.links) {
this.userData.user_type = data1.type;
this.appService.getCleverData(data1.links[1].uri, this.accessToken).subscribe(data2 => {
if (data2.data) {
this.userData.name = data2.data.name.first + ' ' + data2.data.name.last;
this.userData.clever_id = data2.data.id;
this.userData.email = data2.data.email;
this.appService.updateData('Users/cleaver_login', this.userData).subscribe(data3 => {
if (data3.detail) {
console.log(data3.detail);
}
});
}
});
}
});
}
});
}
this is service code
getCleverData(url, token) {
let reqHeader = new HttpHeaders({
'Authorization': 'Bearer ' + token
})
return this.http.get(API_PREFIX + url, { headers: reqHeader })
.pipe(
map((data: any) => {
console.log(data);
if (data) return data;
}),
catchError(this.handleError)
);
}
/** PUT: update a data to the server */
updateData (url, data?) {
let httpParams = new HttpParams();
Object.keys(data).forEach(function (key) {
httpParams = httpParams.append(key, data[key]);
});
return this.http.post(this.apiUrl + url, httpParams, httpOptions)
.pipe(
map((data: any) => {
if (data.status == 0) {
this.presentToast(data.message);
}
if (data) return data;
}),
catchError(this.handleError)
);
}
is there any way to avoid that many subscribe. I can't remove any of it because its some of it from our server and others are third party