Search code examples
javascriptes6-promise

"Then" is not executed after the Promise


I want to coordinate the test of a database with the close. First, I want to do the test and then close the connection and show a test. But, I only get to do the test and not the close.

I have a class where I managed the connection with MySQL, in the file "db.js":

import Sequalize from "sequelize";
import config from "./config";

class MySQLConnector {
    constructor(){
        this.db = new Sequalize(config.ddbb_connection.database, config.ddbb_connection.user,
        config.ddbb_connection.password, {
            host: config.ddbb_connection.host,
            dialect: "mysql",
            operatorsAliases: false,
            pool: {
                max: 5,
                min: 0,
                acquire: 30000,
                idle: 10000
            }
        }) 
    };

    test(){
        this.db.authenticate().then(() => {
            console.log("Connection to database: " + config.ddbb_connection.database + 
            " has been established succesfully");
        })
        .catch(err => {
            console.log("Unable to connect to database: " + err);
        }); 
    };

    closeConnection(){
        this.db.close();
        console.log("Connection to database: " + config.ddbb_connection.database + 
        " has been closed!!!");
    }
};

module.exports.MySQLConnector = MySQLConnector;

And I have another file "index.js" where I try to make the test and then close the connection:

import express from "express";
import config from "./config";
import {MySQLConnector} from "./db";

const app = express();
let db = new MySQLConnector();
let myPromise = new Promise((resolve, reject)=>{
    db.test();
});

myPromise.then(()=>{
    db.closeConnection();
}).catch(()=>{
    console.log("Don't do anything!!!");
});

app.listen(config.server.port, () => {
    console.log("Server " + config.server.name + " listening on port " + config.server.port);
});

But, the only thing that I get is to make the test and not close the connection. You can check the result in this screen capture:

enter image description here

What am I doing wrong?

Edit I:

If I call "then" when a create the promise, it doesn't works. I've got the same result.

let db = new MySQLConnector();
let myPromise = new Promise((resolve, reject)=>{
    db.test();
}).then(()=>{
    db.closeConnection();
});

Edit II:

Thank you to @CertainPerformance to give me the correct solution!!!.

My code finally is

db.js:

import Sequalize from "sequelize";
import config from "./config";

class MySQLConnector {
    constructor(){
        this.db = new Sequalize(config.ddbb_connection.database, config.ddbb_connection.user,
        config.ddbb_connection.password, {
            host: config.ddbb_connection.host,
            dialect: "mysql",
            operatorsAliases: false,
            pool: {
                max: 5,
                min: 0,
                acquire: 30000,
                idle: 10000
            }
        }) 
    };

    test(){
       return this.db.authenticate().then(() => {
            console.log("Connection to database: " + config.ddbb_connection.database + 
            " has been established succesfully");
        })
        .catch(err => {
            console.log("Unable to connect to database: " + err);
        }); 
    };

    closeConnection(){
        this.db.close();
        console.log("Connection to database: " + config.ddbb_connection.database + 
        " has been closed!!!");
    }
};

module.exports.MySQLConnector = MySQLConnector;

index.js:

import express from "express";
import config from "./config";
import {MySQLConnector} from "./db";

const app = express();
let db = new MySQLConnector();
db.test().then(() => db.closeConnection());

app.listen(config.server.port, () => {
    console.log("Server " + config.server.name + " listening on port " + config.server.port);
});

The result what I get is what I expect!!! First I make the test, then I close the connection.

enter image description here


Solution

  • In index.js you're constructing a Promise but never calling its resolve function, so it never resolves. But there's no need for the Promise constructor here: simply have test return the Promise chained from authenticate:

    test(){
      return this.db.authenticate().then(() => {
      // ...
    

    and then in index.js:

    db.test()
      .then(() => db.closeConnection());
    

    If you did use the Promise constructor method (which you definitely shouldn't because you already have a Promise, but just for information), index.js would look like this, calling the resolve argument so as to resolve the Promise once test is done:

    let myPromise = new Promise((resolve, reject)=>{
      db.test()
        .then(resolve);
    });
    
    myPromise.then(()=>{
      db.closeConnection();
    }).catch(()=>{
      console.log("Don't do anything!!!");
    });