Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
NodeJs: Is there a way to exit the whole function instead of just the internal function with return?

So I have this function

app.post('/assignment/loan', (req, res) => {

And inside that function I have this function

db.run('SELECT loanable FROM book WHERE id=?',[bookID],(err,row)=>{

I use return but it only exits the internal function and keeps on going with the rest. I want to stop the whole post function from executing further. Is there a way to do that?

Edit: Full code here: https://pastebin.com/8TPThfpW

Thank you in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Not directly. By the time your run callback is running, the function that called it has already returned. This is because of the nature of JavaScript's run-to-completion semantics and the fact that Node.js callbacks of this kind are called asynchronously.

You might consider using promise wrappers around those functions, and then putting your logic in an async function so that you can make the function's logic wait until the run operation and your handling of its results are complete. It's hard to give you a concrete suggestion with such fragmentary code to work from, but something vaguely like this:

// In an `async` function...
const res = await promiseEnabledAppPost('/assignment/loan');
const row = await promiseEnabledDBRun('SELECT loanable FROM book WHERE id=?',[bookID]);
// ...do something with `row`...
if (/* you don't want to continue with the `post` callback logic */ {
    return;
}
// ...continue the logic from the `post` callback...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!