Function call in CoffeeScript

Here’s a little hassle in CoffeeScript syntax. Calling a function with parameter but without parentheses is fine.

Below sample code works.

express = require 'express'
app = express()
app.configure ->
  app.use express.bodyParser()
  app.use express.methodOverride()
  app.use app.router

app.get '/', (req, res) ->
  res.send { ok: true }

app.listen 3000

Calling a function with no parameter and parentheses is bad.

Same sample code except one little difference will cause the app to hang!! Notice that there is no parenthesis used on methodOverride.

express = require 'express'
app = express()
app.configure ->
  app.use express.bodyParser()
  app.use express.methodOverride #no parentheses
  app.use app.router

app.get '/', (req, res) ->
  res.send { ok: true }

app.listen 3000

So remember always use parentheses when a function has no parameter and it will save you time on debugging weird problems suddenly showing up in your codes.

About rp8

Specialized in building sophisticated systems for trading & risks in commodity, exotics, commodity index & structured products. Specially interested in using open source stacks and cloud computing to build new generation of services and apps. Enjoy mountain biking & photography. github
This entry was posted in Node.js and tagged , . Bookmark the permalink.