0

Is there a way that we can return the value from a promise? I have this async/await function meant to retrieve the token from another async/await function getToken() but it only returns a promise string to me. However, it does return the intended value in console log. What would be the best workaround for me to access the value itself and not a promise string like in the picture below?

const retToken = async(): Promise<any> => {
  const value = await getToken();
  console.log('value: ', value);
  return value;
}

console.log('retToken: ', retToken());

console log results

Zam Abdul Vahid
  • 2,389
  • 3
  • 18
  • 25
Falady
  • 124
  • 1
  • 15

1 Answers1

1

You forgot to await the asynchronous function.

console.log('retToken: ', await retToken());

Remember that asynchronous functions have to be awaited in order for them not to return a pending promise.

Nicholas
  • 2,800
  • 1
  • 14
  • 21
  • Everytime i added await before the call it throws this error `Error: Objects are not valid as a React child (found: object with keys {_U, _V, _W, _X}). If you meant to render a collection of children, use an array instead.` and this warning from react native `Error: Invalid hook call. Hooks can only be called inside of the body of a function component.` – Falady Apr 03 '21 at 00:18