Last active
January 3, 2016 03:28
-
-
Save philipwalton/8402139 to your computer and use it in GitHub Desktop.
Async events handling strategies
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
// host library code | |
program() | |
.initStuff() | |
.then(function() { | |
dispatcher.emit('beforeRenderPost') | |
}) | |
.then(function() { | |
dispatcher.emit('afterRenderPost') | |
}) | |
.then(function() { | |
dispatcher.emit('beforeWritePost') | |
}) | |
.then(function() { | |
dispatcher.emit('afterWritePost') | |
}) | |
.catch(errorHandler) | |
// now the user's code can simply be the following and the host logic | |
// will wait until their async function is finished before continuing | |
dispatcher.on('beforeRenderPost', doSomethingAsyncToPost) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I originally considered this. Like, if the callback function's
.length
property is one greater than the emitter'sarguments.length
property, assume async, but that won't work 100% of the time. Sometimes a callback will make use of the arguments object for...rest
parameter type situations, so it gets a bit hairy.