0

I need the function getAUTH to return a value that i'll later assign to variable 'AUTH' but it returns Promise {pending}. This is my code:

const axios = require('axios')
const { getinfobipAuth } = require('../secrets-manager')

//get infobipAUTH from secret manager
const getAUTH = async () => await getinfobipAuth()
const AUTH = getAUTH()
console.log(AUTH)

axios.defaults.headers.common['Authorization'] = AUTH

Tried resolving the promise like: const AUTH = getAUTH().then(res => res) But assigning the value AUTH still doesn't return a value. Can a variable declared outside a function be assigned a value from a promise?

I'll appreciate any help with this.

SabetiG
  • 379
  • 2
  • 8
  • 2
    Async functions always return Promises; you can't get a value *synchronously* which is retrieved *asynchronously*. The only way is to have the caller call `.then` on the Promise, or for the caller to `await` the Promise to unwrap it – CertainPerformance Sep 28 '20 at 14:03
  • Hi @CertainPerformance I have called `.then` on the caller of `getAUTH() ` as this `const AUTH = getAUTH().then(res => { return res }) console.log(AUTH)` but I still have the log as `Promise{}` – SabetiG Sep 28 '20 at 14:21

0 Answers0