I am using nodejs and the node-mysql driver. I find myself doing repetitive boiler plate such as
client.query(querystr, queryparams, function callback(err, results, fields){
if (err){ do err related stuff }
else { do success related stuff }
}
Bottom-line most of the time everything is straightforward and repeated except what happens on success which varies wildly depending on the query and context.
So I’d like to encapsulate all this in a single function and pass in a querystr, queryparams and a success function.
Problem: I can’t do this as this is inside a callback and I cant make the query engine use my success function even if I pass it in via the callback arguments.
So I am thinking can I do a “require” inside the callback and if so will the functions in the “required” module be added to the global scope or the callback scope? I am hoping the latterB
You can assign variables to module scope, which is an approximation of ‘global’ like this: