Undefined result from node-mssql query
<p>Well, I'm new to NodeJS. Although I have some basics knowledge, now I'm a little bit stuck with tihs code below. The goal would be to create a common module which can execute the given SQL query and return its result.</p>

<p>The problem is pretty clear. My method returns earlier than the result could come back from the SQL server. I tried to solve it in dozen ways, but none of them worked. I really appreciate if somebody could explain what I did wrong and what's the proper way to make this code works.</p>

<p>database \ config.js</p>

<pre><code>module.exports = {
    user: 'mssql',
    password: 'mssql',
    server: 'localhost',
    database: 'mssql',
    stream: true
};
</code></pre>

<p>database \ database.js</p>

<pre><code>var sql = require('mssql');

var config = require('./config');

module.exports = function(query) {

    var result = [];

    sql.connect(config).then(function () {
        var request = new sql.Request();
        request.query(query);

        request.on('row', function (row) {
            result.push(row);
        });
    }).then(function() {
        return result;
    });
}
</code></pre>

<p>routes \ index.js</p>

<pre><code>var express = require('express');
var router = express.Router();

var queries = require('../database/queries.json');
var database = require('../database/database');

router.get('/', function (req, res, next) {
    res.render('index', {
        books: database(queries.books)
    });
});

module.exports = router;
</code></pre>