Created
April 24, 2012 07:53
-
-
Save yocontra/2477645 to your computer and use it in GitHub Desktop.
Asynchronous object map Javascript/node/coffee-script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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