question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wait for all queries to complete?

See original GitHub issue

I’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:closed
  • Created 7 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
sidorarescommented, Jun 30, 2016

with await you could have something like this to execute in parallel (maybe not in one line and without crazy destructuring ):

  [  [ [fullStruct.server.timeOffset] ], [ [fullStruct.server.timeZone] ] ] =
      await Promise.all([ 
        pool.query("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) offset"),
        pool.query("SELECT IF(@@session.time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone) timeZone") ]);         

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 doing getConnection() + 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.

0reactions
sidorarescommented, Aug 15, 2016

Which has been fixed?

This one:

It doesn’t appear to be releasing the connection when there’s an error

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found