Created
August 24, 2012 20:31
-
-
Save imcotton/3455299 to your computer and use it in GitHub Desktop.
jQuery pipe in CoffeeScript
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
each = (list) -> | |
return $.Deferred().resolve 'done' if not list?.length | |
t = new Date | |
$.post('/echo/json/', json: list.shift()).pipe (n) -> | |
console.log "round (#{n}) in #{ new Date - t }ms" | |
each list | |
each([0..3]).done (res) -> | |
console.log "I've #{res}." | |
# output below: | |
### | |
round (0) in 254ms | |
round (1) in 263ms | |
round (2) in 252ms | |
round (3) in 245ms | |
I've done. | |
### | |
# compiled JavaScript below: | |
-> | |
` | |
var each; | |
each = function(list) { | |
var t; | |
if (!(list != null ? list.length : void 0)) { | |
return $.Deferred().resolve('done'); | |
} | |
t = new Date; | |
return $.post('/echo/json/', { | |
json: list.shift() | |
}).pipe(function(n) { | |
console.log("round (" + n + ") in " + (new Date - t) + "ms"); | |
return each(list); | |
}); | |
}; | |
each([0, 1, 2, 3]).done(function(res) { | |
return console.log("I've " + res + "."); | |
}); | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment