我有一个NodeJs10 Lambda函数来解析数据库中的条目并将其保存回数据库中。当我在本地运行时,它将解析所有条目。当我在Lambda函数中运行它时,它仅解析3-4个条目。我最好的猜测是Lambda以不同的方式处理异步调用,并认为所有条目都已解析。
如何让我的Lambda函数在关闭之前运行所有条目?
我的代码全局如下:
'use strict';
// Packages
var Mercury = require('@postlight/mercury-parser');
var Mysql = require('mysql');
// All necessery sql Queries
var sqlSelect = "Select some stuff";
var sqlUpdateText = "Update some stuff";
exports.handler = async function(event,context) {
// Define the database used in pools
var pool = Mysql.createPool({
});
// Promise where the links and id's are fetched from the database
let fetchLink = new Promise((resolve,reject) => {
pool.getconnection((err,connection) => { // Connection with the database
});
});
// After getting the links and id's,parse the text from the links and put them in a JSON object
return fetchLink.then(async function (dbresult) {
if (dbresult.length > 0) {
await parser();
}
return textObj;
})
// After parsing
.then(function (textObj) {
if (Object.getOwnPropertyNames(textObj).length !== 0) {
pool.getconnection((err,connection) => {
if (err) throw err;
for (let id in textObj) {
// Do some db stuff
}
connection.release();
pool.end();
});
} else {
pool.end();
}
})
// Catch the error message's
.catch(function (err) {
console.log(err);
pool.end();
});
};