Skip to content

Instantly share code, notes, and snippets.

@imcotton
Created August 24, 2012 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imcotton/3455299 to your computer and use it in GitHub Desktop.
Save imcotton/3455299 to your computer and use it in GitHub Desktop.
jQuery pipe in CoffeeScript
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