0

I have this Promise snippet,

    let myPromise = new Promise(async function(resolve, reject){

    const defaultConfig = {
            s3: {
                region: process.env.ENV_S3_REGION,
                accessKeyId: await juice.string('S3.ACCESS_KEY_ID', juice.MANDATORY),
                secretAccessKey: process.env.ENV_S3_SECRET_ACCESS_KEY
            },
            uploadConfig: { 
                bucket: process.env.ENV_UPLOAD_BUCKET
            }
}
   resolve(defaultConfig);
}

And I'd like to module.export the defaultConfig object. So I did this at the end of the code,

myPromise.then(res => module.exports = res)

But when calling in on other file, it can't get the value, it gives me undefined. But if I console.log the res like, this I can output the value.

myPromise.then(res => console.log(res))
quielfala
  • 361
  • 4
  • 18
  • Relevant: [Is it an anti-pattern to use async/await inside of a new Promise() constructor?](https://stackoverflow.com/q/43036229) – VLAZ Apr 27 '22 at 14:03
  • 2
    The export happens before the promise resolves and you can't make it wait. Export the promise instead. – Quentin Apr 27 '22 at 14:04
  • @Quentin, I see. That would be a hassle in my current code. – quielfala Apr 27 '22 at 14:28
  • If I export the promise itself, then in the importing file I'd refactor the code, and move everything inside the .then so I can get the value right? – quielfala Apr 27 '22 at 14:30

0 Answers0