Created
July 30, 2011 17:52
-
-
Save devongovett/1115785 to your computer and use it in GitHub Desktop.
Asynchronous if statements in CoffeeScript
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 Async | |
constructor: -> | |
@stack = [] | |
@IF: (condition, fn) -> | |
async = new Async | |
async.stack.push (next) -> | |
if condition then fn(next) else next() | |
process.nextTick async.run.bind(async) | |
return async | |
ELSEIF: (condition, fn) -> | |
@stack.push (next) -> | |
if condition then fn(next) else next() | |
return this | |
ELSE: (fn) -> | |
@stack.push fn | |
return this | |
run: -> | |
stack = [@stack...] | |
do next = -> | |
fn = stack.shift() | |
fn(next) | |
module.exports = Async |
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
Async.IF somecondition, -> | |
doSomething() | |
.ELSEIF someothercondition, -> | |
doSomethingElse() | |
.ELSE -> | |
anotherThing() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment