Created
June 19, 2012 16:38
-
-
Save Radagaisus/2955173 to your computer and use it in GitHub Desktop.
Some Coffee Sugar for Async
This file contains hidden or 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
| async = require 'async' | |
| # Async needs some coffee script suger | |
| ### | |
| Parallel | |
| --------------------------------------------------- | |
| // an example using an object instead of an array | |
| async.parallel({ | |
| one: function(callback){ | |
| setTimeout(function(){ | |
| callback(null, 1); | |
| }, 200); | |
| }, | |
| two: function(callback){ | |
| setTimeout(function(){ | |
| callback(null, 2); | |
| }, 100); | |
| }, | |
| }, | |
| function(err, results) { | |
| // results is now equals to: {one: 1, two: 2} | |
| }); | |
| async.parallel -> | |
| @fn 'one': -> | |
| setTimeout => | |
| @callback null, 1 | |
| 200 | |
| @fn 'two': -> | |
| setTimeout => | |
| @callback null, 2 | |
| 100 | |
| @callback -> | |
| console.log @results, @err | |
| # @results is now equal to {one: 1, two: 2} | |
| # @err is null | |
| ### | |
| @parallel = (fn) -> | |
| @_callback = -> # noop | |
| @callback = (fn) -> | |
| @_callback = (@err, @results) -> | |
| @err = err | |
| @res = @response = @results | |
| fn.apply this, [@err, @results] | |
| @_func_obj = {} | |
| @fn = (o) -> | |
| for k,v of o | |
| do (k,v) => | |
| @_func_obj[k] = (@callback) -> | |
| console.log "hey" | |
| @cb = @c = @callback | |
| v.apply this, [@callback] | |
| fn.apply this | |
| console.log @_func_obj, @_callback | |
| async.parallel @_func_obj, @_callback | |
| @parallel -> | |
| @fn 'one': -> | |
| console.log "one" | |
| @callback null, 1 | |
| @fn 'two': -> | |
| console.log "two" | |
| @callback null, 2 | |
| @callback -> | |
| console.log @results, @err | |
| # @results is now equal to {one: 1, two: 2} | |
| # @err is null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment