mongoose async await code example

Example 1: async await mongoose connection

(async () => {
  try {
    await mongoose.connect(dbURI, dbOptions)
  } catch (err) {
    console.log('error: ' + err)
  }
})()

Example 2: node js mongoose async await

const connectToDb = async () => {
  try {
    await mongoose.connect(<connection_string>, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
    });
    console.log("Db connection success");
  } catch (err) {
    console.log(err.message);
    process.exit(1)
  }
};

Example 3: dynamodb async await

try {
    const data = await dynamodb.put(paramsPut).promise()
    console.log("Success")
    console.log(data)
    return data
} catch (err) {
    console.log("Failure", err.message)
    // there is no data here, you can return undefined or similar
}