Alexis
Alexis

Reputation: 25163

If statement with callback design pattern in coffeescript

I would like to do something like this in Coffeescript

data = [default, data]
if has_some_data (err, data)->  #has some data hits the db and is a callback
   data = data.get_some_data()
process_data()

I would like it so that process data is only called when after the if statement has executed. Is there a good pattern to do this in CoffeeScript?

Upvotes: 1

Views: 68

Answers (1)

Eric Wendelin
Eric Wendelin

Reputation: 44349

I'm not super-sure that I'm answering your question, but if your question is "How might I write CoffeeScript that accomplishes this goal?" then...

What about using a callback and errback like so:

data = {...}
callback = (data) -> process_data(data) if data
errback = (err) -> console.log(err)
has_some_data(args, callback, errback)

Otherwise, could you clarify what your constraints are here so I can actually answer your question? ;)

Upvotes: 2

Related Questions