0

I'm trying to get info from this promise, and store the values into a variable that I can use anywhere

Here's the function that returns the promise

const getInfo = async () => {
  let info;

  await Promise.all([new myApi().getTiers()])
    .then(([apiResponse]) => {
      info = apiResponse;
    }).catch(error => {(
        console.log(error))
    });

  return info;
}

Now in a different function, the furthest I've gotten is

const export myInfo () => {
    getInfo().then(r => console.log(r))
}

This isn't really useful to me as r is an array that I need to manipulate. I also can't make myInfo an async function as it's in a react component

Everything else I try just reusults into Promise<pending>

  • Why are you doing `await info`? Is your API call's response somehow a promise as well? – M0nst3R Jul 28 '20 at 10:33
  • 3
    You're just doing very complicated contortions that could be reduced to `const getInfo = () => new myApi().getTiers()`. This will return a promise, **and there's nothing you can do to change that.** The caller of `getInfo` must accept that and `await`/`.then` it, and so must *its* caller, and its caller, and its caller. Turtles all the way down. React surely can handle that, if done correctly. – deceze Jul 28 '20 at 10:35
  • @GhassenLouhaichi that's actually a typo, edit question –  Jul 28 '20 at 10:37
  • 1
    You can always use an async IIFE such as `(async () => { // await something here })();` but it's normal to deal with async functions in React. What specifically is preventing you? – jarmod Jul 28 '20 at 10:38

0 Answers0