1

I'm trying to understand how parameters are already assigned when passed into callback functions. For example, in the mysql npm package Connection.js the .connect method passes a callback with an err paramaeter. I'm trying to understand the step by step details of how the .connect callback function err param is initialized and assigned.

mysql.connect(function(err) {
  if (err) {
    console.error('error connecting: ' + err.stack);
  }
  console.log('connected as id ' + connection.threadId);
});

My understanding of the connect call is this.

First, mysql.connect is invoked, which is a reference to

Connection.prototype.connect = function connect(options, callback)

in \node_modules\mysql\lib\Connection.js

Then, the code inside the connect function executes and ends with the line

this._protocol.handshake(options, bindToCurrentDomain(callback));

Control is then passed back to the anonymous callback function(err), which can now reference the err object since it has now been assigned.

Is this correct?

If not, any insight into this would be appreciated. In particular, I'm trying to understand

1) What is happening step by step during the execution of the call to .connect and how it exectues then hands control back to the anonymous callback.

2) How the anonymous callback has access to the assigned err param.

3) Is the answer to #2 essentialy the pattern (in essence of what occurs, not exact implementation details) for how callbacks with params "auto assigned"? (e.g. req and res params in express.Router().route(function(req,res)...)

Thanks

lance-p
  • 1,050
  • 1
  • 14
  • 28
  • 1
    Somewhere in the code something is doing `callback(error);` where `error` is either `null` or an actual error object. You just need to find the place where `callback` is actually called. But it's no different than calling any other function. There is no magic "auto assignment". Take `function sum(a, b) { return a + b; }` as example. If I call it with `sum(1,2)` then `a` and `b` will be assigned `1` and `2`. The difference between my example and your callback example is only that is not *you* who calls the callback, but some library code (`mysql.connect`). – Felix Kling Apr 04 '17 at 21:55
  • This is where the callback is ultimately called I believe: https://github.com/mysqljs/mysql/blob/eeca1326c5f4d67725a486ddf1ef12c3f12d2187/lib/protocol/sequences/Sequence.js#L86 – Felix Kling Apr 04 '17 at 22:01
  • In your mysql.connect you are providing only a function. In your Connection.prototype.connect there's an options object before the callback function. If mysql.connect == Connection.prototype.connect I would expect it to fail since err == your function ? The err argument is private the the function it is defined in, and since the only way out for it would be to have it returned by the function. – user3094755 Apr 04 '17 at 22:04
  • This might help: [JavaScript: Passing parameters to a callback function](http://stackoverflow.com/q/3458553/218196) – Felix Kling Apr 04 '17 at 22:05
  • Thanks. I guess it essentially does something similar to this ultimately: function connect(callback){ var error = 'none'; //try to connect and set error if no connection var success = {items:[1,2,3]}; callback(error, success); } connect(function(err, succ){ if(err !== 'none') { console.log(err); } console.log(succ); }); – lance-p Apr 05 '17 at 01:17

0 Answers0