1

I am having an issue figuring out how to make an array of functions with different parameters. I am using coffeescript and async but I think it is a problem with my understanding of javascript.

I want to create an array of functions with different tasks.

names = ['Jeff', 'Maria', 'Steve']
tasks = []

for name in names
  tasks.push (callback)=>
      @controller.get_person name, (person) =>
        callback(null, person)

async.parallel(tasks, cb)

The problem is that the task is getting called with Steve (always the one that is last in the array) three times. How do I make it so that there is one task per each name?

3

2 Answers 2

1

Actually, in this particular case, you should probably make use of async's map:

getPerson = (name, callback) =>
  @controller.get_person name, (person) ->
    callback(null, person)

async.map names, getPerson, (err, persons) ->
  // Do stuff with persons

Note that if your @controller.get_person method followed the node practice of passing any errors as the first parameter to the callback, this would suffice:

async.map names, @controller.get_person, (err, persons) ->
  // Do stuff with persons

Something to keep in mind, perhaps.

Sign up to request clarification or add additional context in comments.

2 Comments

very nice! Thank you. The first way works beautifully. The second way looks awesome. However, in the second way, the @controller does not seem to get passed through to the map function. What I mean by this is that the @ seems to get rebound and looses the reference to controller. Is there a way to fix this?
@Scoop: Yes, you need to declare get_person with the fat arrow (=>) so it will always be bound to the object (in the class definition of whatever @controller is).
1

Try changing for name in names to names.forEach (name)=>. Be careful of the space after forEach.

names = ['Jeff', 'Maria', 'Steve']
tasks = []

names.forEach (name)=>
  tasks.push (callback)=>
      @controller.get_person name, (person) =>
        callback(null, person)

async.parallel(tasks, cb)

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.