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

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

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 Denunciar

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 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