我正在使用nodejs环境开发AWS Lambda。我有一个 API,其中我使用递归函数来实现某些功能。
实际上大多数人都说避免递归函数,但根据我的功能,我需要这个。我的功能如下。
我有一个表table1
,其中有两列pid和cid,它们被定义为唯一约束。这意味着两列的组合应该是唯一的。
因此,如果我在 table1
中插入任何组合,并且该组合已经存在,那么它会给我重复输入错误,根据我的功能,这是正确的。
因此,为了处理这个重复条目错误,我使用了 try..catch block 。因此,在 catch block 中,我检查了是否发生重复条目错误,然后调用一个递归函数,该函数尝试不同的组合,直到在 table1
中创建新条目。
我的递归函数是基于 promise 的。但是当成功创建新条目时,我就兑现了 promise 。但是 Promise 并没有从我第一次调用递归函数的地方返回。因此会发生超时。
所以请有人建议我解决方案,以便我的 promise 得到解决,并且我的功能将从我第一次调用递归函数的点继续。所以超时不会到来。
我提供我的代码供引用。
var mysql = require('mysql');
var con = mysql.createConnection({
"host": "somehost.com",
"user": "myusername",
"password": "mypassword",
"database": "mydatabase"
});
exports.handler = async (event, context) => {
try {
con.connect();
} catch (error) {
throw error;
return 0;
}
let tableId = '';
let count = '';
try {
var tempUsageData = {
user_id: userId,
code: code,
platform: source,
some_id: some_id,
count: count
};
dbColumns = 'user_id, code, platform, added_on, count, some_id';
let usageData = [
[userId, code, source, new Date(), count, some_id]
];
var tableInsert = await databaseInsert(con, constants.DB_CONSTANTS.DB_USAGE, dbColumns, usageData);
tableId = tableInsert.insertId;
} catch (error) {
console.log('@@ error insert table1 @@', error);
if (error.errno == 1062) {
try {
// calling recursive function here
let newTableData = await createTableEntry(con, tempUsageData);
tableId = newTableData.new_usage_id;
count = newTableData.new_count;
} catch (error) {
console.log('Error', error);
return 0;
}
} else {
return 0;
}
};
console.log('@@ EXECUTION DONE @@');
return 1;
}
var createTableEntry = (con, dataObject) => {
return new Promise(async function (resolve, reject) {
console.log('createTableEntry Called for count', dataObject.count);
try {
var newCounter = await getDataFromDatabase(con, dataObject.some_id);
dbColumns = 'user_id, code, platform, added_on, count, some_id';
let tableData = [
[userId, code, source, new Date(), Number(newCounter[0].counter + 1), some_id]
];
var tableInsert = await databaseInsert(con, 'table1', dbColumns, tableData);
let response = {
new_table_id: tableInsert.insertId,
new_count: Number(newCounter[0].counter + 1)
}
return resolve(response);
//function not returning from here once successful entry done and timeout occures
} catch (error) {
console.log('@@ ERROR @@', error);
if (error.errno == 1062) {
console.log('@@ CALL FUNCTION AGAIN @@');
dataObject.count = Number(newCounter[0].counter + 1);
await createTableEntry(con, dataObject);
} else {
return reject(error);
}
}
});
};
我的最终输出应该是消息“EXECUTION DONE”,执行完成后应该显示。
请给我建议一个好的解决方案。提前致谢。
最佳答案
更新你的 catch block
catch (error) {
console.log('@@ ERROR @@', error);
if (error.errno == 1062) {
console.log('@@ CALL FUNCTION AGAIN @@');
dataObject.count = Number(newCounter[0].counter + 1);
let result = await createTableEntry(con, dataObject);
return resolve(result);
} else {
return reject(error);
}
}
关于javascript - 基于 promise 的递归函数中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57093142/