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

196
Views
How to return multiple statement in angular?

I am creating Login page and want to call /login and /send-otp api together by a single function. I have already created a login and registration page.

My code :

user-data.service.ts :

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
 
@Injectable({
  providedIn: 'root'
})
export class UserdataService {

  url= 'http://localhost:9197/register';
  url2= 'http://localhost:9197/login';
  url3= 'http://localhost:9197/send-otp';

  constructor(private http:HttpClient) {}

  saveRegistration(data:any){
    return this.http.post(this.url, data);
  }

  loginDone(ldata:any){
    return this.http.post(this.url2, ldata);
    return this.http.post(this.url3, ldata);
  }

}

How to call multiply api ??

 loginDone(ldata:any){
    return this.http.post(this.url2, ldata);
    return this.http.post(this.url3, ldata);
  }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Apply TypeScript alias definition to your code:

Here in a more simplificated way, you have a multiple return with an alias structure.

type multi={
x:PelotaAzul,
y:PelotaRugosa,

}
function multiple():multi{
let x:PelotaAzul=new PelotaAzul("jj",5);
let y:PelotaRugosa=new PelotaRugosa("s",3,6)
let ab : multi ={x,y};
return ab;

}

You can also return an array of the both parameters like this

 loginDone(ldata:any){
    return [this.http.post(this.url2, ldata),this.http.post(this.url3, ldata)];
  }

I find creating aliases more elegant but for faster results do the second one.

about 4 years ago · Juan Pablo Isaza Report

0

You are probably miss thinking your problem.

First of all. A return statement will always stop the function execution, doesn't matters if theres 100 lines after it.

Second, your logic probably will be: call login first then after (using or not the response) call the send-otp.

Or, your logic might be: call login and send-otp concurrently, but the loginDone method should only return some valid state if both requests run fine.

That's why other answers are completely wrong.

You must cascade your observables using pipe and switchmap for example:

return of([this.url2, this.url3]).pipe(
  switchMap((url) => this.http.post(url, ldata))
)
about 4 years ago · Juan Pablo Isaza Report

0

If you want to return multiple values, you can return like this.

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
 
@Injectable({
  providedIn: 'root'
})
export class UserdataService {

  url= 'http://localhost:9197/register';
  url2= 'http://localhost:9197/login';
  url3= 'http://localhost:9197/send-otp';

  constructor(private http:HttpClient) {}

  saveRegistration(data:any){
    return this.http.post(this.url, data);
  }

  loginDone(ldata:any){
    return {url1: this.url, url2: this.url2, url3: this.url3};
  }
}

If you want to call multiple api, check this one .

https://medium.com/bb-tutorials-and-thoughts/how-to-make-parallel-api-calls-in-angular-applications-4b99227e7458

about 4 years ago · Juan Pablo Isaza 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!