2

trying to wrap my head around some basic async programming concepts.

Right now I essentially have two functions, for now lets call them getOne and getAll. getAll essentially loops of a series and makes a call to getOne, then maps it to a hash and sends it back down the chain. Here is basically what I have, in coffeescript:

getOne = (key, callback) ->
    try
        # find object referenced by key
        object = "blah blah"
        callback(null, object)
    catch(error)
        callback(error, null)

getAll = (keys, callback) ->
    results = {}
    try
        count = keys.length
        for i, key of keys
            count--
            getOne key, (err, result) ->
                if(err)
                   # do something
                else
                   results[key] = result
                if count is 0
                   callback "", results
    catch
        callback error, results

The above code essentially outputs a hash where the keys don't necessarily lineup with the expected data, which is expected the way a for loop is async processed. However, I can't seem to come up with a solution that doesn't block the loop and cause everything to be processed in a series instead of maintaining parallel processing.

What am I missing?

2 Answers 2

3

There isn't anything inherently asynchronous about the code you've written. If you're worried about the callbacks in getOne taking a long time to execute, you have a couple of options.

The first option is definitely easier.

The second option involves rewriting your getAll function as a recursive function (instead of using for) and wrapping recursive subcalls in process.nextTick.

This article is reasonably useful for understanding how to use process.nextTick to accomplish this: Understanding process.nextTick()

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

Comments

2

Using the async library, it's as simple as this:

getAll = (keys, callback) ->
  funcs = {}
  keys.forEach (key) => funcs[key] = (_callback) =>
    getOne key, _callback
  async.parallel funcs, callback

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.