0

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*
Mil0R3
  • 3,484
  • 4
  • 29
  • 56

2 Answers2

1

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
qiao
  • 16,270
  • 5
  • 55
  • 46
1

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?

Community
  • 1
  • 1
topr
  • 4,072
  • 3
  • 22
  • 33