Last active
December 17, 2015 20:39
-
-
Save MikeBild/5669512 to your computer and use it in GitHub Desktop.
playing with .bind and sequencing async apply
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
var Flow = function(){ | |
this.args = Array.prototype.slice.call(arguments); | |
var next = function(){ | |
if(this.args.length>0){ | |
this.args.shift(); | |
this.args[0].apply(next, Array.prototype.slice.call(arguments)); | |
} | |
}.bind(this); | |
this.args[0].apply(next); | |
} | |
new Flow( | |
function(){ | |
slow("A", this); | |
console.log("1"); | |
}, | |
function(data){ | |
slow2(data + "B", "!!!", this); | |
console.log("2"); | |
}, | |
function(data, data2){ | |
slow(data + "C" + data2, this); | |
console.log("3"); | |
}, | |
function(data){ | |
console.log("4"); | |
console.log(data); | |
} | |
); | |
function slow(value, callback){ | |
setTimeout(function(){ | |
callback(value); | |
}, 1000) | |
} | |
function slow2(value, value2, callback){ | |
setTimeout(function(){ | |
callback(value, value2); | |
}, 500) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment