0

I'm trying to assign a variable to an immediately invoked async function but cannot resolve the promise it returns.

const user: any = (async () => {
        try {
            return JSON.parse(await redis.get(email) || '{}')
        } catch (error) {
            console.log('Failed to parse JSON of redis value, error:', error)
            return {}
        }
    })().then(token => {return token})

console.log(user) prints Promise { <pending> }, I've read all the answers on here related to this subject and can't figure out how to make this work, if it's even possible/ advisable. Thanks.

Thomas Huber
  • 21
  • 2
  • 4
  • 1
    If you wrap your code in a main async function, then you can await your IIAFE (or not use it at all), without it you cant await a promise before your console.log fires – Lawrence Cherone Mar 29 '22 at 03:20
  • @LawrenceCherone wow thanks! It actually was in an async function, I should've included that. I didn't realize I could simply put await before the IIAFE. – Thomas Huber Mar 29 '22 at 03:25

0 Answers0