Skip to content

Instantly share code, notes, and snippets.

@mertenvg
Created July 14, 2015 12:00
Show Gist options
  • Save mertenvg/5cceaacc9fd10f31388c to your computer and use it in GitHub Desktop.
Save mertenvg/5cceaacc9fd10f31388c to your computer and use it in GitHub Desktop.
Chain the actions and callbacks so with the final callback is the final function called in the chain. fw -> f1 -> fw -> f2 -> fw -> f3 -> callback
var gtmActions = {
"a": function (callback) {
console.log("a");
window.setTimeout(callback, 500)
},
"b": function (callback) {
console.log("b");
window.setTimeout(callback, 500)
},
"c": function (callback) {
console.log("c");
window.setTimeout(callback, 500)
},
"d": function (callback) {
console.log("d");
window.setTimeout(callback, 500)
}
}
function wrapCallback(fn, callback) {
return function () {
fn(callback);
}
}
var actions = ['a','b','c','d'],
fn = function () {
console.log("done");
};
while ((a = actions.pop())) {
if (gtmActions.hasOwnProperty(a)) {
fn = wrapCallback(gtmActions[a], fn);
}
}
fn();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment