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

354
Views
Node and async: Why is await not being honored in a sub-function?

I'm a Node.js student, and I'm writing a brief example. The get on /org works as expected.

With the get on /orgg I tried to segregate the server "fetch the data" logic from the controller. However, the getRowsAsync() immediately returns a Promise.

This "put the await in express.get()" isn't good for hiding logic. If my biz logic needed some sequential Promises then the biz logic would have to bubble into the controller.

How can I do the equivalent to calling "doBizLogic()" in the controller, hiding my awaits, and having the controller wait for the logic to complete? Must I pass a callback function to the biz logic to make this scheme work?

Here is my index.js. I omit database.js, but I borrowed it from https://mhagemann.medium.com/create-a-mysql-database-middleware-with-node-js-8-and-async-await-6984a09d49f4

const express = require("express");
const urlPort = 3000;
let app = express();
let mysql = require("mysql2");
let pool = require("./database");

app.listen(urlPort, () => {
  console.log("Server is running at port " + urlPort);
});

app.get("/", (req, res) => {
  res.send("This app responds to /org and /org/:id only.");
});

app.get("/org", async (req, res) => {
  let rows = await getRows("select * from org");
  // the log always prints a JSON array.
  console.log("in app.get for /org, rows: ", rows);
  res.send(rows);
});

app.get("/orgg", (req, res) => {
  let rows = getRowsAsync();
  // the log() prints immediately prints a Promise.
  console.log("in app.get for /orgg, rows: ", rows);
  res.send(rows);
});

function getRows(sql, params = []) {
  let rows = pool.query(sql, params);
  // the log() prints a Promise.
  console.log("in getRows, rows: ", rows); // returns 
  return rows;
}

async function getRowsAsync() {
  let rows = await getRows("select * from org");
  // the log() prints a JSON array, once it is finally called.
  console.log("in getRowsAsync, rows: ", rows);
  return rows;
}

over 4 years ago · Santiago Trujillo
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!