Created
February 14, 2012 02:56
-
-
Save corbanbrook/1823000 to your computer and use it in GitHub Desktop.
load stepper for async functions
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
| var a = function(callback) { console.log("A"); callback("Hello from A"); }; | |
| var b = function(response, callback) { if (!response) { throw "Err: no response"; } console.log("B", response); callback(); }; | |
| var c = function(callback) { console.log("C"); callback(); }; | |
| var d = function() { console.log("D"); }; | |
| var e = function(callback) { console.log("E"); callback(); }; | |
| var f = function() { console.log("F"); }; | |
| var load = (function() { | |
| var funcs = []; | |
| var index = 0; | |
| var _Stepper = { | |
| run: function() { | |
| this.next(); | |
| }, | |
| next: function() { | |
| var func = funcs[index]; | |
| if (func && typeof func.func === "function") { | |
| console.log("Running <" + func.name + ">"); | |
| index++; | |
| func.func.apply(this, arguments); | |
| if (!func.async) { | |
| _Stepper.next.apply(this, arguments); | |
| } | |
| } else { | |
| console.log("END"); | |
| } | |
| }, | |
| add: function(name, func, async) { | |
| funcs.push({ name: name, func: func, async: async }); | |
| } | |
| }; | |
| return _Stepper; | |
| }()); | |
| load.add("A", function() { a(load.next); }, true); | |
| load.add("B", function(response) { b(response, load.next); }, true); | |
| load.add("C", function() { c(load.next); }, true); | |
| load.add("D", function() { d(); }); | |
| load.add("E", function() { e(load.next); }, true); | |
| load.add("F", function() { f(); }, true); | |
| load.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment