我正在lambda上测试claudia api,我只想打开与mysql服务器的连接并返回连接是否成功。我有一个hello world,可以在其中简单地通过api返回字符串。每当我尝试创建一个Promise,然后在该Promise中打开一个连接,然后用结果解析Promise时,Promise就永远不会解析,并且Lambda函数会超时。我知道这意味着我永远不会解决所创建的承诺,但是我不确定在连接代码完成后如何正确解决它。在连接aysnc代码完成之后,如何等待解决承诺?
const ApiBuilder = require('claudia-api-builder')
var api = new ApiBuilder()
const mysql = require('mysql');
var connection = mysql.createConnection({
host: 'myserverurl',
port: '3306',
user: 'username',
password : 'password',
database: 'db'
});
var sql = "SELECT * FROM UserStatus"
api.get('/test', function (request) { // GET all users
var results = ""
return new Promise (function (resolve,reject) {
connection.connect(function(err) {
if (err) {
results = "fail"
}
results = "connected"
resolve(results)
})
resolve("I know that I need to resolve the promise here, but not sure how to make this wait for connection to finish")
});
});
module.exports = api;