1

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

3 Answers 3

3

Asynchronous constructor is potentially an antipattern, exactly because it doesn't provide proper control flow when an instance is created. It's expected that an instance is ready to use when it's created with new but it isn't ready.

A proper way to handle this is to have asynchronous hooks that are executed outside the class:

export class App {
    constructor() {}

    async init() {
        console.log("a1");
        await this.config();
        console.log("a2");
    }

    async config() {
       ...
    }
}

...

const app = new App();
await app.init();
Sign up to request clarification or add additional context in comments.

2 Comments

it worked, thank you very much, but when I instantiate "new Class ()" it gets async again, so it ends up running the code from below and then starting the mongo
@wellington.silverio Yes, it's asynchronous, because promises and async are in use. I'm not sure why this is a problem. const app = new App(); await app.init() should result in a sequence you wanted.
0

You can make use of Promise.then (because async functions implicity return Promises), stuff the things you want done after your config() in its then callback.

constructor() {
  console.log("a1");
  this.config().then(_ =>
    console.log("a2");
  });
}

2 Comments

it worked, thank you very much, but when I instantiate "new Class ()" it gets async again, so it ends up running the code from below and then starting the mongo
Code: console.log("1"); const app = New App(); console.log("2"); Response: 1 a1 b1 a2 2 MongoDB Running b2
0

A good way is to learn about Promises

The good way to go could be to return in the config a Promise and after that you can use the then function in the Promise.

2 Comments

it worked, thank you very much, but when I instantiate "new Class ()" it gets async again, so it ends up running the code from below and then starting the mongo
Code: console.log("1"); const app = new App(); console.log("2"); Response: 1 a1 b1 a2 2 MongoDB Running b2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.