Skip to content

Instantly share code, notes, and snippets.

@yocontra
Created April 24, 2012 07:53
Show Gist options
  • Save yocontra/2477645 to your computer and use it in GitHub Desktop.
Save yocontra/2477645 to your computer and use it in GitHub Desktop.
Asynchronous object map Javascript/node/coffee-script
map = (obj, cb) ->
out = {}
todo = Object.keys(obj).length
check = -> cb out if --todo is 0
for key, val of obj
if typeof val is 'function'
do (key) ->
t = val (res) ->
out[key] = res
check()
if t?
out[key] = t
check()
else
out[key] = val
check()
return
test =
taskA: (cb) ->
cb "Some cool async result"
return
taskB: "Some cool sync value"
taskC: -> "Some other cool sync result"
map test, (res) ->
###
{
taskA: 'Some cool async result',
taskB: 'Some cool sync value',
taskC: 'Some other cool sync result'
}
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment