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

179
Views
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 answers
Answer question

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 Report

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