Skip to content

Instantly share code, notes, and snippets.

@EtienneLem
Last active December 16, 2015 09:59
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 EtienneLem/5416815 to your computer and use it in GitHub Desktop.
Save EtienneLem/5416815 to your computer and use it in GitHub Desktop.
Custom Deferrer. It doesn’t support errors, because, you know, fix them… You shouldn’t code errors to begin with.
doThis = (deferrer) ->
# do some JS nifty things
deferrer.done('foo', 'bar')
doThat = (deferrer) ->
# Have callbacks/timeouts/async? Sure!
setTimeout ->
deferrer.done('foozle', 'barzle')
, 5000
new Deferrer [doThis, doThat], (data) ->
# Happens after 5 seconds
console.log(data)
# => { foo: 'bar', foozle: 'barzle' }
class Deferrer
constructor: (actions, @callback) ->
@queue = actions.length
@data = {}
for action in actions
action(this)
done: (key, value) ->
@queue--
@data[key] = value
this.callback(@data) if @queue is 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment