Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda