In some modules I saw this strange way of initializing variable used in a callback.
This particular example is from mssql module:
var sql = require('mssql');
var connection = new sql.Connection(config, function (err) {
var request = new sql.Request(connection);
request.query('select 1 as number', function (err, recordset) {
// do something
});
});
What appears strange to me is that connection is used inside callback as if it is already initialized, and in fact it is.
However I would thought that callback should be run before function sql.Connection() does return. In fact there is no way to run anything after it returns.
So how does this thing work?