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*

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.