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

204
Views
Async function makes it hard to access variable because variable is not "ready" when it is needed

I have problems with my variable keys. getAllKeys saves keys into the variable keys. I want to use keys in prepareDate but when the code gets to prepareDate it does not work because the values are not yet asignt to variable keys (because of this JavaScript Async stuff)

I need to know how I can wait until keys is "ready" so prepareDate (and the rest of the code) can work with keys. There is .then and promise (if this is the right solution) but I do not know how to use them here.

  let keys = [];

  const getAllKeys = async () => {
    try {
      keys = await AsyncStorage.getAllKeys();
    } catch (e) {
      // read key error
    }
    console.log("keys: ", keys) // This works, the keys are saved inside keys
  }

  const getData = async (key) => {
    try {
      const jsonValue = await AsyncStorage.getItem(key);
      return jsonValue != null ? JSON.parse(jsonValue) : null;
    } catch (e) {
      // error reading value
    }
  };

  const prepareData = () => {
    console.log("keys: ", keys)
    keys.forEach((key) => {   // I want that this works but there is nothing inside keys.. 
      console.log("hier")
      console.log(getData(key))
    })
  }

  getAllKeys();
  prepareData();
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Stop using a global variable keys. Instead, return a value from getAllKeys (so that getAllKeys() returns a promise for that), and accept the keys as a parameter to preprateData:

const getAllKeys = async () => {
  try {
    return await AsyncStorage.getAllKeys();
  } catch (e) {
    // read key error
    return [];
  }
};

const prepareData = async (keys) => {
  const data = Promise.all(keys.map(key => {
    console.log(key);
    return getData(key);
  }))
  console.log(data);
  return data;
}

Now you can use them as

getAllKeys().then(prepareData);

or in

(asnyc () => {
  const keys = await getAllKeys();
  console.log("keys: ", keys) // This works
  await prepareData(keys);
})();
about 4 years ago · Juan Pablo Isaza Report

0

You could postpone the call to prepareData until after getAllKeys is finished, like so:

getAllKeys().then(prepareData);

We can do that because getAllKeys is an async function and thus returns a Promise, which allows us to call the then-method on it.

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!