Wait for MySQL query in async function?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Node.js, waiting for a MySQL query in an async function means using a promise-based driver and await at every step that affects control flow. If a query is not awaited, later logic runs too early and bugs appear as race conditions or empty results. A production-safe pattern also includes connection pooling, proper transaction handling, and predictable error propagation.
This is especially important in API services where one missing await can cascade into data races under concurrent load.
Use a Promise-Based MySQL Driver
The cleanest approach is mysql2/promise, which returns promises directly.
The important part is that both createConnection and query are awaited.
Prefer a Pool for Real Applications
Opening a new connection per request does not scale. Pools reuse connections and reduce latency.
This pattern is easier to integrate into web servers because pool lifecycle is application-wide.
Transactions Require Await on Every Step
For related writes, await begin, each query, commit, and rollback.
If one of these awaits is missing, transaction guarantees can break silently.
Integrate with Express Route Handlers
Async route handlers should await query calls and pass errors to middleware.
This keeps control flow explicit and avoids unhandled promise rejections.
Timeout and Slow Query Protection
Waiting forever is a common operational issue. Configure query and socket timeouts based on service expectations. Also monitor slow query logs in MySQL so application-level awaits do not hide database performance problems.
Useful guardrails:
- keep indexes aligned with query predicates
- return limited result sets
- measure per-query latency in logs
- alert on timeout spike rates
Awaiting correctly gives correctness, but good query design gives reliability.
Graceful Shutdown and In-Flight Queries
When the process receives termination signals, stop taking new requests and close pool cleanly. This prevents orphaned in-flight work during deploys.
Define shutdown behavior early so async query handling stays predictable during restarts.
Test for Missing Await Bugs
Integration tests should verify both success and failure paths.
A missing await often appears as flaky tests with intermittent zero-row reads after writes.
Common Pitfalls
A common mistake is mixing callback APIs and promise APIs in the same module. Keep one async style end to end.
Another mistake is forgetting to await transaction commit or rollback. This can leave connections in bad state.
A third mistake is creating one connection per request instead of using a pool, causing avoidable overhead.
Teams also ignore cleanup paths during errors and shutdown, leading to leaked connections and unstable service restarts.
Summary
- Use promise-based MySQL APIs and await every query tied to program flow.
- Prefer a shared connection pool for web services.
- In transactions, await begin, query calls, commit, and rollback explicitly.
- Propagate errors predictably in async route handlers.
- Add timeout, logging, and shutdown guardrails for production reliability.
Related reading
- WAL shipping priority?
- Warning about SSL connection when connecting to MySQL database
- Warning Accessing non-existent property 'MongoError' of module exports inside circular dependency
- Warning mysql_connect 2002 No such file or directory trying to connect via unix///tmp/mysql.sock in
- Wait for promise in a forEach loop
- Wait until all threads are finished in Python
- Waiting for async function in React component Showing Spinner
- Waiting for async function to return true or false - how do I check return value?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.