I'm trying to run 'Mongoose' and only continue my task when it's connected, but what happens is that the task runs first and then connects the Mongo
export class App {
constructor() {
console.log("a1");
this.config();
console.log("a2");
}
public async config() {
// Connect to MongoDB
console.log("b1");
try {
await mongoose.connect(stringConnection, { useNewUrlParser: true }).finally();
console.log("MongoDB Running");
} catch (error) {
console.log(error);
process.exit();
}
console.log("b2");
}
}
Answer:
a1
b1
a2
MongoDB Running
b2
Answer I wanted:
a1
b1
MongoDB Running
b2
a2