Catch Block In Recursive Function Executing Repeatedly
Solution 1:
Your code contains many obvious misunderstandings of how to write practical and robust asynchronous programs in JavaScript. There are still a few things I would change about the code below but I cannot advise as information about codecommit.getFile
and runScripts
are not provided. If you have any questions about this answer, I am happy to help -
asyncfunctiongetFileData(files) {
const result = []
for (const f of files) {
try {
const data = await codecommit.getFile({
filePath: f.relativePath,
repositoryName: 'testDBScripts'
}).promise()
awaitrunScripts(data)
result.push(f.relativePath)
}
catch (e) {
thrownewError("SQL file " + f + " failed with : " + e.message)
}
}
return result
}
Using it looks like this -
getFileData(newSQLFiles)
.then(console.log, console.error)
.finally(_ => client.release())
.finally(_ => pool.end())
Or if you prefer catch
, this does the same thing -
getFileData(newSQLFiles)
.then(console.log)
.catch(console.error)
.finally(_ => client.release())
.finally(_ => pool.end())
Note .finally
callback can also return a promise that properly sequences the program. See the example below -
constdelay = (ms,x) =>
newPromise(r =>setTimeout(_ =>console.log(x) || r(x), ms))
delay(1000,"a")
.then(_ =>delay(1000,"b"))
.then(_ =>delay(200, "result"))
.finally(_ =>delay(500,"client released"))
.finally(_ =>delay(1000,"pool closed"))
.then(console.log, console.error)
a
b
result
client released
pool closed
result
If any promise in the sequence is rejected or an error is thrown, the .finally
handlers are still called -
constdelay = (ms,x) =>
newPromise(r =>setTimeout(_ =>console.log(x) || r(x), ms))
delay(1000,"a")
.then(_ =>Promise.reject(Error("SQL FAILURE")))
.then(_ =>delay(200, "result"))
.finally(_ =>delay(500,"client released"))
.finally(_ =>delay(1000,"pool closed"))
.then(console.log, console.error)
a
client released
pool closed
Error: SQL FAILURE
Solution 2:
In JavaScript, when a function hits a return
statement, it will stop the function form executing furthermore.
Probably it's currently not working because right after the catch
within the function, you are returning.
So, to solve your issue, I would do like so:
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();
awaitrunScripts(data);
processed.push(newSQLFiles[0].relativePath)
}catch(err){
console.log(err)
throw [err,processed];
}
awaitgetFileData(newSQLFiles.slice(1),processed);
}
awaitgetFileData(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)thrownewError("SQL file " + file + " failed with : " + e)}
)
Note that I've removed the return
from the function's scope after the try catch
block.
Post a Comment for "Catch Block In Recursive Function Executing Repeatedly"