How to override default value of parameter?

I have the following function signature:

question: (question_id, onComplete, use_redis = true) ->

I assume that I can override the use_redis parameter to be false, but everything I have tried hasn't worked yet. Is this possible to do if I am defining my onComplete callback inline? This is what my call to the function looks like:

Summaryresults.question 1, (summary) ->
    answer_data.summary = summary
    done()

I've tried adding false several different ways, but nothing works. Do I need to define my callback function somewhere else and then reference that rather than defining it inline in order to override use_redis?

should work that way:

Summaryresults.question 1, (summary) ->
  answer_data.summary = summary
  done()
, false

the first param is the id, the second your callback function, the third setting use_redis to false

edit - my solution for changing the api:

question: (id, options...) ->
  [use_redis, onComplete] = options
  if typeof use_redis is "function"
    onComplete = options
    use_redis = true

and you should always put your callback as the LAST argument when calling this function:

without use_redis-param:

Summaryresults.question 1, (summary) ->
  answer_data.summary = summary
  done()

with use_redis-param:

Summaryresults.question 1, false, (summary) ->
  answer_data.summary = summary
  done()

Typically, you want a callback to be the last argument, because the syntax gets wonky otherwise.

So why not change your API?

question: (question_id, options = {}) ->
  onComplete = options.onComplete
  useRedis = options.useRedis ? true
  # ...

Summaryresults.question 1,
  useRedis: false
  onComplete: ->
    answer_data.summary = summary
    done()

See how much more readable that is?


But that said, you can invoke with a funky comma in just the right spot

Summaryresults.question 1, (summary) ->
  answer_data.summary = summary
  done()
, false

Which compiles to:

Summaryresults.question(1, function(summary) {
  answer_data.summary = summary;
  return done();
}, false);

Or Perhaps

hollaback = (summary) ->
  answer_data.summary = summary
  done()

Summaryresults.question 1, hollaback, false