Skip to content

Instantly share code, notes, and snippets.

@MikeBild
Last active December 17, 2015 20:39
Show Gist options
  • Save MikeBild/5669512 to your computer and use it in GitHub Desktop.
Save MikeBild/5669512 to your computer and use it in GitHub Desktop.
playing with .bind and sequencing async apply
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