是否可以从 .then 函数导出数据?

Is it possible to export data from .then function?

我正在使用 mssql 模块处理 Node 和 MSSQL Server。使用承诺时,我想从 .then 函数 导出或 return 数据 。这是可能的还是有任何解决方法?

getDb = function(){

// This code establishes connection to SQL Server

  const conn = new sql.ConnectionPool(dbConfig);
  const req = new sql.Request(conn)
  conn.connect()
  .then(function getData (req) {

// This code makes query to SQL Server

      req.query("SELECT * FROM USER")
      .then(function(res){

      console.log(res) // logs Correct User
      module.exports.user = res // logs undefined in main.js

      })
      .catch((err) => console.log(err))
    }
  )
  .catch(function (err) {
      console.log(err);
  });
}
getDb()

非常感谢任何帮助!

这是导出 getDb 函数并将其导入并使用到数据的简单方法:

exports.getDb = () => {

    return new Promise((resolve, reject) => {
      const conn = new sql.ConnectionPool(dbConfig);
      const req = new sql.Request(conn)
      conn.connect().then(req => {
        return req.query("SELECT * FROM USER")
      }).then(res => {
        return resolve(res);
      }).catch(err => {
        console.log(err)
      })
    })
}