Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

338
Visualizações
Getting response from nested Async funtion in React

I have 3 function to login a user on my React web application.

  1. Function C: Calling Login function from Login HTML Page

 const handleLogin = (e) => {
        e.preventDefault();

        //  Calling FUNCTION B
        loginUser({ email, password }).then((value) => console.log('in promise then : ' + value));
    
        console.log('in login page: ' + response);
    };
  1. Function B: Making authorization

export async function loginUser(loginPayload) {

     //  Calling FUNCTION C
     AuthDataService.login(loginPayload)
      .then((response) => {
          var modifiedResponse = response;
          console.log('in AuthDataService: ' + JSON.stringify(modifiedResponse));
          return modifiedResponse;
      });
}
  1. Function A: Calling Server

class AuthDataService {
  async login(data) {
    return await http.post('/login', data).then((response) => {
        return response;
    });
  }
}

Problem is that, in Function B response is logged correctly but in Function A response(value) is undefined. Shouldn't function C wait until Function B is finished? What should I change?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are not returning anything from loginUser function.

NOTE: No need for async if are using .then

Refactoring you can do like below

export function loginUser(loginPayload) {
  //  Calling FUNCTION C
  return AuthDataService.login(loginPayload).then((response) => {
    var modifiedResponse = response;
    return data; // not sure from where it's coming
  });
}

class: no need to then if you are not doing anything with response.

login(data) {
    return http.post('/login', data);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

First: in handleLogin you're not assinging anything to the response value. Second: in loginUser and login you're not returning any value.

you can try this:

const handleLogin = async (e) => {
  e.preventDefault();

  const response = await  loginUser({ email, password }).then((value) => console.log('in promise then : ' + value));

  console.log('in login page: ' + response);
};

export const loginUser = async (loginPayload) => {
  return AuthDataService.login(loginPayload).then((response) => JSON.stringify(response));
}

class AuthDataService {
  async login(data) {
    return await http.post('/login', data)
  }
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda