Mil0R3
Mil0R3

Reputation: 3956

callback function with a parameter

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*

Upvotes: 0

Views: 142

Answers (2)

topr
topr

Reputation: 4612

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?

Upvotes: 1

qiao
qiao

Reputation: 18219

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

Upvotes: 1

Related Questions