Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6202629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:46:13+00:00 2026-05-24T04:46:13+00:00

Currently, this is how I perform a query using node-mysql client.query( sql, function( error,

  • 0

Currently, this is how I perform a query using node-mysql

client.query( sql, function( error, result ) {
  console.dir( result );
});

I would like to do this synchronously, something like this

var result = client.querySync( sql );
console.dir( result );

I understand why blocking in node is bad, but I’m (almost) grown up enough to know when it’s okay and when it’s not. I only intend to make synchronous calls at the initialisation stage, outside of any event loops.

Does anybody know how I can achieve this please?

Edit…

Something along the lines of…

client.querySync = function( sql )
{
    var called = false;
    var result;

    while ( typeof result == 'undefined' ) {
        if ( ! called ) {
            called = true;
            this.query( sql, function( error, _result ) {
                result = { error: error, result: _result };
            });
        };
    }

    return result;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:46:13+00:00Added an answer on May 24, 2026 at 4:46 am

    Your proposed solution (in your edit) won’t work because you never give up the thread (so the callback can never be called, so the variable can never be set, so your loop never breaks). Node is not multi-threaded – there is only ever one thread executing javascript at any one time. There is no way to yield that thread except by returning from whatever code is running.

    So, you can’t do what you want to do. You could try to use some of the solutions that re-write your sync code into async behind the scenes, but I’ve personally found that approach isn’t really worth the effort — that’s it better to just bite the bullet and just do everything with callbacks (over time the pain subsides :).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently use this function to wrap executing commands and logging their execution, and
I am currently receiving this error: Validation of viewstate MAC failed. If this application
I am currently testing this in Mozilla FireFox 3.0.5 using FireBug 1.3.0 with jQuery
I'm currently using this jQuery validate plugin and having an issue in IE where
I am using the Django query filter __search to perform a full text search
What is the best way to to perform this query. I have the following
I am using symfony+doctrine, and I want to perform a delete query with a
I asked this question last week on SO- SQL Exclusion Query It looks like
I'm currently trying to perform a search over 2 fields in my MySQL table
I'm using Symfony2 and Doctrine2. How would I go about using a different query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.