I looked through the felixge / node-mysql library and did not see the link to the client.connect command in the API. Is this the real challenge you're trying to make (not trying to be ridiculous here)? Regardless of IMHO, you need to think more about how Javascript is developed because it uses a programming paradigm different from most other popular languages.
The first problem that I see in your code is that you did not define a callback, so it does not actually exist. I would suggest that console.log (callback) is undefined. From your code, the anonymous function is a callback for the client.connect function. You must determine that you are calling a "callback" in a higher scope. For example, I will define the myCallback function to exist in an area higher than the client.connect anonymous function. It may be useful to find Javacscript variable scope .
var myCallback(err, response) { if (err) { console.log('err:%s',err); } else { console.log('response:%s',response); } } client.connect(err, function(response) { // this anonymous function is the callback to client.connect, the var // 'callback' would be undefined. if (err) { myCallback(err); return; // Explicit call to return, else the lines below would run. } myCallback(null, response); });
Secondly, if you do not explicitly call return inside Javascript, the function will continue to be processed. I was bitten by it. Finally, Javascript starts the event-driven loop because it will never wait for functions to return, so we have all these callbacks in the first place. You can make Javascript behave differently, for example, by using a while loop until the condition is true. See the “Asynchronous” caolan library for various event loop management strategies. The main disadvantage of using these methods is that you actually spend CPU cycles / blocking, when you probably should use more callbacks and just rethink how your programs work.
exshovelrydr Apr 29 2018-12-12T00: 00Z
source share