0

Now i created an async function getUsers which returns a promise with the correct data. Then, I tried retrieving the data in a form of array by using .then(). However, the data is empty when I call getUsers() in get().

getUsers = async () => {
  try {
    const users = await AsyncStorage.getItem("@FriendBook:Users");
    if (users !== null) {
      return JSON.parse(users);
    }
    return [];
  } catch (error) {
    console.log("Unable to load users", error);
  }
};

get = () => {
  var p = getUsers();
  var users = [];

  p.then((res) => {
    users = res;
  });

  return users;
};
kmagued
  • 33
  • 1
  • 1
  • 4
  • 1
    If AsyncStorage is an asynchronous function you can’t return users like that. – evolutionxbox May 26 '20 at 10:06
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – evolutionxbox May 26 '20 at 10:06
  • @evolutionxbox well, I already tried using async function and awaiting the response but still no luck – kmagued May 26 '20 at 10:19
  • I don’t know what you mean. Remember I can’t see you screen or the code you’ve used. – evolutionxbox May 26 '20 at 10:25

0 Answers0