【发布时间】:2021-04-01 22:43:31
【问题描述】:
我有一个递归函数,用于从 AWS 上的 CodeCommit 存储库获取 SQL 文件并按顺序运行它们。在运行下一个文件之前,我们需要等待上一个文件完成。如果其中一个 SQL 文件失败,我需要 catch 块来返回有关失败文件的信息。
我目前在代码中看到的是,catch 块对 repo 中的每个 SQL 文件重复一次。据我了解,“throw”语句应该返回到最初调用该函数的函数的 catch 块。谁能指出我在这里做错了什么?
const getFileData = async (newSQLFiles,processed=[]) => {
try{
if(newSQLFiles.length ===0){
client.release();
await pool.end().then(() => console.log('DB Connection pool closed.'))
return processed;
}
var params = {
filePath: newSQLFiles[0].relativePath,
repositoryName: 'testDBScripts' //Use environment variable
};
const data = await codecommit.getFile(params).promise();
await runScripts(data);
processed.push(newSQLFiles[0].relativePath)
}catch(err){
console.log(err)
throw [err,processed];
}
return await getFileData(newSQLFiles.slice(1),processed);
}
await getFileData(newSQLFiles)
.then(processed=>console.log("Following products are updated.",processed))
.catch(async ([e, file])=> {
client.release();
await pool.end().then(() => console.log('DB Connection pool closed.'))
//await codePipelineJobFailed("SQL file " + file + " failed with : " + e)
throw new Error("SQL file " + file + " failed with : " + e)}
)
【问题讨论】:
标签: javascript recursion async-await aws-sdk