Wait for all queries to complete?
See original GitHub issueI’ll start with an example:
const mysql = require('mysql2/promise');
let pool = mysql.createPool({
user: dbVars.login,
password: dbVars.password,
host: 'dev-sql',
connectionLimit: 10,
namedPlaceholders: true,
});
let fullStruct = {
server: {},
databases: {},
};
pool.query("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) offset").then(([timeOffsetRows]) => {
fullStruct.server.timeOffset = timeOffsetRows[0].offset;
});
pool.query("SELECT IF(@@session.time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone) timeZone").then(([timeZoneRows]) => {
fullStruct.server.timeZone = timeZoneRows[0].timeZone;
});
await pool.end();
console.log(fullStruct);
This results in an error:
Error: Pool is closed.
I believe this happens because I’m closing the pool/connection before one of the queries has started. Now I can fix this by pushing each of those query promises into an array and then calling Promise.all
before pool.end
, but littering my code with queries.push(...)
is kind of gross.
Does mysql2 not track the queries internally? Is there no way for me to just say “wait for everything to complete before closing”?
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
node.js - Wait for all query to finish and fill at the same time ...
I want to fill each object of the result of a query, with other querys, and I want to do all in asynchronously...
Read more >Wait for query to finish before returning results
If your query is showing few data before it is completing the query execution, it means that the query is not blocked to...
Read more >Refetching queries in Apollo Client - Apollo GraphQL Docs
refetchQueries method collects the TResult results returned by onQueryUpdated , defaulting to TResult = Promise<ApolloQueryResult<any>> if onQueryUpdated is not ...
Read more >Await for a query to finish - Retool Forum
I have searched posts and think I have tried all suggestions but I know I'm missing something. The GetOrgsOfUser need finish before I...
Read more >node js wait for loop to finish
For Gun Make: Six Shooter -Not all but most. Run-to-completion semantics; 24. Nodejs wait for a for loop to finish with mysql query....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
with await you could have something like this to execute in parallel (maybe not in one line and without crazy destructuring ):
process.nextTick might be not enough - when Promise is returned the promise handler itself might be called in different tick ( can’t tell for sure ), so you have race between promise handlers in
.query()
and.end()
+ query() internally is doinggetConnection()
+connection.query()
asynchronously, so it becomes really hard to tell for sure if this line is called before this one regardless on the original order of high level api calls.This one: