Reputation: 11
I want to modify this function to send these two file ids in the separate requests:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
body.user.profilePic = res.data.profilePic;
body.user.coverPic = res.data.coverPic;
return this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body);
})
);
Should I use flatmap?
Upvotes: 1
Views: 43
Reputation: 96889
The right operator depends on whether you want to send the two request in parallel or consecutively.
If you already have take(1)
then you can use both switchMap
or mergeMap
because it will always emit only once and thus it doesn't matter in this case.
Sending requests in parallel:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
...
return forkJoin([
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
]);
}),
);
Sending requests in sequence:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
...
return concat(
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
);
}),
);
Upvotes: 1
Reputation: 2982
You can do separate requests from one pipe like that:
return this.upload(myForm).pipe(
take(1),
switchMap(res => {
body.user.profilePic = res.data.profilePic;
body.user.coverPic = res.data.coverPic;
return [
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
this.http.post<IRresponse<object>>(environment.api + EndPoint.CreateUser, body),
]
}),
mergeAll(),
);
Upvotes: 1