How can I end an asynchronous call to behave synchronously?

I am currently executing a query using node-mysql

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

I would like to do it synchronously, something like this

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

I understand why the lock in node is bad, but I (almost) grew enough to know when everything is in order and when not. I only intend to make synchronous calls in the initialization phase outside of any event loop.

Does anyone know how I can achieve this?

Edit ...

Something along the lines ...

 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; }; 
+4
source share
5 answers

Your proposed solution (in your editing) will not work because you will never give up the thread (therefore, callback can never be called, so the variable can never be set, so your loop never breaks). Node is not multithreaded - there is only one thread executing javascript at any given time. There is no way to get this stream, except by returning from any code.

So you cannot do what you want. You can try to use some of the solutions that rewrite your synchronization code in async backstage, but I personally found that this approach is not worth the effort - to better just bite the bullet and just do everything with callbacks (over time the pain subsides :).

+5
source

not to do. Learn how to do asynchronous programming.

Use flowcontrol like futuresJS to organize your code

+2
source

I assume that you could add a queuing system, such as an array of commands in the โ€œclientโ€ of commands that are added and then looped on every call to your clientโ€™s synchronous method, as well as at the end of any โ€œlockโ€.

So:

 client.mySyncMethod = function(command, callback) { commandArray.push({command, callback}); runCommands(); } client.runCommands = function() { for (var i in commandArray) { // run command } } 
+1
source

What is the problem you are facing?

Your selection is procedural due to callbacks. If you donโ€™t want anything to start before it all ends, just run the main code after all your callbacks have completed:

 client.query( sql, function( error, result ) { console.dir( result , function () { //now start your app code } ); }); 

Everything is still asynchronous, and you do not fight with your environment until nothing happens if the procedural start procedure is not completed. Wrap it all up with a simple function with a callback as an argument, and you should be good to go.

To do this, everything is synchronous: Node.js obviously does not allow you to stop the event loop. There is no such thing, but you simply cannot. You can write your Sync request as a callback chain to make it behave synchronously .. but it will be ugly.

0
source

There is no way in node.js to make asynchronous code behave like synchronous code.

-1
source

All Articles