Reputation: 1000
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?
Upvotes: 2
Views: 2988
Reputation: 20315
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
Upvotes: 2
Reputation: 20257
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.
process.nextTick
to defer execution of each iteration of your for loopThe 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()
Upvotes: 3