Last active
December 16, 2015 09:59
-
-
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.
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
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' } |
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
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