Reputation: 148514
I have a Lambda function in AWS using Node 8.1 which initiate MSSQL request ( to an external server , which is not in aws)
When I'm using the non async
handler with non async code (but callbacks )—
exports.handler = (event, context, callback) => {...}
— Everything is OK.
Proof: For this code, which uses callbacks-
exports.handler = (event, context, callback) => {
sql.connect(config, err => {
if (err) {
callback(err);
} else {
const req = new sql.Request();
req.query(
'select * from img.dbo.images where imageid = 1641',
(error, result) => {
if (error) {
callback(error);
} else {
sql.close();
callback(null, result.recordset);
}
}
);
}
});
};
I get this response :
However , If I change the code to the async version :
exports.handler = async (event, context, callback) => {
try {
let pool = await sql.connect(config);
let result1 = await pool
.request()
.query('select * from img.dbo.images where imageid = 1641');
callback(null, result1.recordset);
} catch (err) {
callback(err);
}
};
— I get a Timeout error(sure it's related to a non-fulfill promise) :
Question
Why doesn't the async
version work ? how can I make it work?
Upvotes: 2
Views: 2652
Reputation: 82096
Remove the callback param and just return the result instead. Also, you can avoid catching errors unless you need to do any special handling
exports.handler = async (event, context) => {
const pool = await sql.connect(config);
return await pool
.request()
.query('select * from img.dbo.images where imageid = 1641');
}
Upvotes: 4