How Promises work in backend code — Basic
A Promise represents async work that will finish later (database, file, network). Knex queries return promises; Express handlers use async/await to read results cleanly.
Learn: Before async/await, Promises were the standard way to handle async results in Node. Knex still returns a Promise from every query builder chain. Understanding then/catch helps you read older examples; writing new code with async/await is usually clearer in controllers and services.
Supplementary examples
Promise.all for parallel queries
const [users, pastes] = await Promise.all([
knex("users").select("*"),
knex("pastes").select("*"),
]);Course example
// Knex returns a Promise
const promise = knex("users").select("*");
promise
.then((rows) => console.log(rows))
.catch((err) => console.error(err));
// Same query with async/await (preferred in controllers/services)
async function listUsers() {
const rows = await knex("users").select("*");
return rows;
}
Additional references & examples
- MDN: Using Promises
Promise states, then/catch, and chaining.
- Node.js documentation
Official Node APIs: modules, events, and runtime behavior.