Created
April 9, 2011 23:47
-
-
Save Qard/911901 to your computer and use it in GitHub Desktop.
Build callback chains without ridiculous indentation!
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
function chainer(){ | |
var chain = []; | |
function add(callback) { chain.push(callback); } | |
function next() { if (chain.length) { chain[0](); chain.shift(); } } | |
return { add: add, push: add, run: next, start: next, next: next }; | |
} | |
// Make a new chain. | |
var chain = new chainer(); | |
// Add a function | |
chain.add(function(){ | |
console.log('First!'); | |
chain.next(); | |
}); | |
// Add a function | |
chain.add(function(){ | |
console.log('Second!'); | |
}); | |
chain.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment