callback function with a parameter

Problem

I know how to write a callback function with coffee-script,like this:

test1.coffee

exports.cube=(callback)-> 
    callback(5)

test2.coffee

test1=require('./test1')

test1.cube (result) ->
    console.log(result)

I want to know how to add a parameter into callback function? so that I can use it like this:

test1.cube(para,result)->
    //use *para* to compute a *result*
    //here can do something with *result*
Problem courtesy of: Mil0r

Solution

If I understand you correctly, what you want is this:

cube = (x, callback) ->
  callback(x * x * x)

cube 3, (result) ->
  console.log 'the cube of 3 is ', result
Solution courtesy of: qiao

Discussion

You can use built-in methods apply() or call() like

callback.call(...)
callback.apply(...)

Here is more about how and the difference between them: What is the difference between call and apply?

Discussion courtesy of: topr

This recipe can be found in it's original form on Stack Over Flow.