Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Send separate requests instead of one whole in Angular

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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(),
);
over 4 years ago · Santiago Trujillo Report

0

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),
    );
  }),
);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!