This is a common mistake among async / nodejs newbies. You essentially wrapped an asynchronous function inside a synchronization function that destroys the nature of the node event loop. The returned expression must be replaced by a callback. See below:
// Method function getUserInfo (userID, dynamicField, callback) { var query = connection.query('SELECT '+dynamicField+' from users WHERE userID = '+connection.escape(userID)); query.on('result', function(row) { callback(null, row.dynamicField); }); }; // Implementation getUserInfo(8, userEmail, function(err, result){ console.log(err || result); });
By convention, in Nodejs, we always pass the error object first in the callback. In this case, since there is no error to capture, we pass null to its place.
source share