"然后"在承诺之后不执行

时间:2018-05-25 20:03:11

标签: javascript es6-promise

我想用close来协调数据库的测试。首先,我想进行测试,然后关闭连接并显示测试。但是,我只能进行测试,而不是结束。

我有一个类,在那里我管理与MySQL的连接,在文件" 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;

我还有另一个文件" index.js"在哪里我尝试进行测试,然后关闭连接:

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);
});

但是,我唯一得到的是进行测试而不是关闭连接。您可以在此屏幕截图中查看结果:

enter image description here

我做错了什么?

编辑I:

如果我打电话给"然后"当创造承诺时,它不起作用。我得到了相同的结果。

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

编辑II:

感谢@CertainPerformance给我正确的解决方案!!!。

我的代码最终是

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);
});

我得到的结果就是我所期待的!首先,我进行测试,然后关闭连接。

enter image description here

1 个答案:

答案 0 :(得分:2)

index.js中你构建了一个Promise但从不调用它的resolve函数,因此它永远无法解析。但是这里不需要Promise构造函数:只需让test返回从authenticate链接的Promise:

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

然后在index.js

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

如果你 使用了Promise构造函数方法(你肯定不应该因为你已经有了Promise,但只是为了获取信息),index.js看起来像这样,调用resolve参数,以便在test完成后解析Promise:

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

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