I'm trying to migrate from JavaScript to CoffeeScript. However I'm not sure about the best way to optimize the code generated by js2coffee.
Below is the original JavaScript source :
var async = require('async');
var context = {};
context.settings = require('./settings');
async.series([setupDb, setupApp, listen], ready);
function setupDb(callback)
{
context.db = require('./db.js');
context.db.init(context, callback);
}
function setupApp(callback)
{
context.app = require('./app.js');
context.app.init(context, callback);
}
// Ready to roll - start listening for connections
function listen(callback)
{
context.app.listen(context.settings.http.port);
callback(null);
}
function ready(err)
{
if (err)
{
throw err;
}
console.log("Ready and listening at http://localhost:" + context.settings.http.port);
}
And below is the generated CoffeeScript :
setupDb = (callback) ->
# Create our database object
context.db = require("./db")
# Set up the database connection, create context.db.posts object
context.db.init context, callback
setupApp = (callback) ->
# Create the Express app object and load our routes
context.app = require("./app")
context.app.init context, callback
# Ready to roll - start listening for connections
listen = (callback) ->
context.app.listen context.settings.http.port
callback null
ready = (err) ->
throw err if err
console.log "Ready and listening at http://localhost:" + context.settings.http.port
async = require("async")
context = {}
context.settings = require("./settings")
async.series [setupDb, setupApp, listen], ready
Now, I'm not sure if the context
variable is required in CoffeeScript. In the JavaScript source its function is to share the common set of setting across the various components of the application such as database and the settings. Will a proper use of the =>
operator in CoffeeScript help ? If yes how ?