Search code examples
javascriptasynchronouspromiseasync-awaitreturn

How to return value inestead promise in this function?


I'm trying to make the connection with MongoDB database but I can't to export database object, inestead export a promise.

What I would be missing?

index.js

export const db = async() => { 
  return await MongoClient.connect(MONGO_URL)
}

I have also tried this way:

export const db = async() => { 
  const result = await MongoClient.connect(MONGO_URL)
  return result
}

resolvers.js

import { db } from '/mongodb'

This function returns Async Function db


Solution

  • try this:

    const db = async() => { 
      return await MongoClient.connect(MONGO_URL)
    }
    
    export const dbResult=db()
    

    Then

    import { dbResult} from './mongodb'
    dbResult.then(res=>{
         //see what they are
         //console.log(dbResult,res)
    })