0

I am wondering about implementation of functionality like the following one.

As an input I receive a function, which could be either synchronous:

externalFunction = () ->
  return true

or asynchronous:

externalFunction = (done) ->
  done(true)

So, I want to distinguish those types of functions

When I receive a synchronous function, I want to call it and then use its results:

result = externalFunction()
doSomething result

And when I receive an asynchronous one, I want to wait for its callback first:

externalFunction (result) ->
  doSomething result

What is the best way to do it?

Right now I'm wondering about something like this:

promise(externalFunction(myCallback)).complete (err) -> ...
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
petomalina
  • 2,020
  • 2
  • 19
  • 25

1 Answers1

0

This may be the simple answer, but still hoping for better one.

myCallbackAfterFunction = (result) ->
  ...

functions = [...]
for func in functions
  if func.length != 0
    func(myCallbackAfterFunction)
  else
    result = func()
    myCallbackAfterFunction(result)
petomalina
  • 2,020
  • 2
  • 19
  • 25
  • Am I right that you have an external function and you don't know if it is synchronous or asynchronous, so you want to support both variants? – Leonid Beschastny Feb 17 '15 at 15:49