Hello, I’m working with async, await and promises from within nodejs/express and I have this issue: In the catch block after my await, I want the return to also cause the parent function (in this case it’s a GET handler function) to return as well. However it doesn’t during a rejected promise from await, and code still executes as such:
app.route('/theRoute').get(function (req, res) {
let result = await getPromiseThatRejects(data)
.catch((error) => {
res.send(error);
return;
})
//additional code that I don't want executed if promise got error
console.log("In the case of an error, this code still executes when I don't want it to")
})
This is the only solution I found:
app.route('/theRoute').get(function (req, res) {
let success = true
let result = await getPromiseThatRejects(data)
.catch((error) => {
success = false
res.send(error);
})
if (!success) return
console.log("Code no longer executes if there was an error, which is my goal")
})
So basically I just have to check the boolean after the await is done and return if it’s false. But is there a more eloquent way to do this without having to use extra lines for boolean checks, or am I stuck with this technique for now.