Why isn’t console.log() printing when used in .then() while setting up Mongoose connection?

With the following code, nothing is printed to the terminal. I thought that since an await function returns a promise, once it is resolved, the code in the .then() will execute. I know that the promise is successful because nothing is printed to the terminal, which means the .catch() function did not run.

const mongoose = require("mongoose");

main().catch((err) => console.log(err));

async function main() {
    await mongoose.connect("mongodb://localhost:27017/movieApp")
    .then(() => {
         console.log("CONNECTION OPEN");
    })
}

Answer

Either use then or async/await:

const mongoose = require("mongoose");

async function main() {
    await mongoose.connect("mongodb://localhost:27017/movieApp");
    console.log("CONNECTION OPEN");
}

main().catch((err) => console.log(err));

The async/await is a special pattern useful to simplify the coding of asynchronous calls.