1

In fileA, I can do

@someAPI = SomeAPI()
@someAPI.getUser '123'

then in fileB

class SomeAPI

  constructor: (options = {}) ->
    unless @ instanceof SomeAPI
      return new SomeAPI(options)

  getUser: (id) ->
    someAPI.get 'users/show', { 'id': id }, (err, data, res) ->
      if data
        console.log data.name

but is there some way to have the callback returned, so I can do the handling in fileA?

fileA pseudocode

...

processUser: (id) ->
  @someAPI.getUser id, (err, data, res) ->
    if data
      console.log data.name

processUser '123'

fileB pseudocode

...

getUser: (id) ->
  return someAPI.get 'users/show', { 'id': id }, (err, data, res)

This is for a Meteor app where fileA is part of the app, fileB part of a package.

1
  • Is this on the client or server? Oct 1, 2014 at 13:27

2 Answers 2

1

Why don't you think the other way around and pass the callback to fileB ?

getUser: (id, callback) ->
  return someAPI.get 'users/show', { 'id': id }, callback

Then in your app code, pass the callback as an argument to perform result handling locally :

processUser: (id) ->
  @someAPI.getUser id, (err, data, res) ->
    if data
      console.log data name

This is what you did so I'm not sure I understand your concern.

4
  • Thanks, works great. I'm a newb to callbacks in general so missing some of the basics. ^^
    – jiku
    Oct 1, 2014 at 13:02
  • Except what if the callback structure is different? I.e. if I have to do some tinkering in the getUser method before returning it.
    – jiku
    Oct 1, 2014 at 13:07
  • 1
    getUser is just a wrapper around some api call which just take parameters and a callback so I'm not sure what tinkering you could reasonably do in getUser that wouldn't best take place BEFORE the call to getUser, in the api caller context... please edit your question with a clear example if you want a better answer from me.
    – saimeunt
    Oct 1, 2014 at 13:20
  • I'll make a new question with a clear example since I think your answer covered what I originally asked about.
    – jiku
    Oct 1, 2014 at 15:04
1

Since you're on the server, you can use fibers to write this in a synchronous fashion. For example, using futures:

# fileB
Future = Npm.require("fibers/future")

class SomeAPI
  constructor: # ...
  getUser: (id) ->
    # Create a Future object
    fut = new Future()

    # Fire off an API call; when it finishes we store the result in
    # the Future object
    someAPI.get 'users/show', { 'id': id }, (err, data, res) ->
      if err
        fut.throw(err)
      else
        fut.return([data, res])

    # Wait for the Future to resolve and return the value we stored in it
    return fut.wait()

# Usage in fileA
# This is synchronous. If an error occurred, you will get an exception
[data, res] = @someApi.getUser(id)
console.log(data.name)

If you ever want to call out to the API in a Meteor method, you'll probably have to use fibers.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.