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

170
Visualizações
How to store the value of async function to a global variable in react?

So I have a use case of fetching emails of people logged in using the below code and pass it to the backend DB. Is there a way to store the output of the below code (expected scenario shown in subsequent code) in a separate variable and use it later in my code? Not sure if this is a possible scenario.

Note: I am able to print the email in the console, but not sure how to store it in a variable as shown in my second code snippit.

Auth.currentSession()
.then(data => {
let idToken = data.getIdToken();
let email = idToken.payload.email;
console.log(email); 
  })
.catch(err => console.log(err));

Expectation:

    var fetch = Auth.currentSession()
    .then(data => {
    let idToken = data.getIdToken();
    let email = idToken.payload.email;
    console.log(email); 
      })
    .catch(err => console.log(err));
  
  
async function addData() {
  const data = {
    body: {
      account_id: formState.account_id,
      builder: fetch,
    },
  };

  console.log(data);
  const apiData = await API.post("testapi", "/test", data);
  console.log({ apiData });
  alert("Request Submitted");
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

@t_killah - Thank you, I was later able to resolve this using window.email.

 Auth.currentSession()
  .then((data) => {
    let idToken = data.getIdToken();
    window.email = idToken.payload.email;
  })
  .catch((err) => console.log(err));

async function addData() {
  const data = {
    body: {
      builder: window.email,
    },
  };
about 4 years ago · Juan Pablo Isaza Relatório

0

You can store it in context, and then your data will be available globally. https://reactjs.org/docs/hooks-reference.html#usecontext

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