Created
April 23, 2012 10:31
-
-
Save Raynos/2470137 to your computer and use it in GitHub Desktop.
Flow - Sequential control flow
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
// After.flow https://github.com/Raynos/after.js#flow | |
function flow(array, context) { | |
array.reduce(function (memo, outer) { | |
return function (inner) { | |
memo.call(context, function () { | |
var args = [].slice.call(arguments).concat(inner) | |
outer.apply(context, args) | |
}) | |
} | |
})() | |
} |
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
// http://jsfiddle.net/cxy4p/15/ | |
var stack = [ | |
function (next) { | |
console.log(1) | |
console.log(this.foo === "bar") | |
next("foo") | |
}, | |
function (foo, next) { | |
console.log(2) | |
console.log(this.foo === "bar") | |
console.log(foo === "foo") | |
next("bar") | |
}, | |
function (bar, next) { | |
console.log(3) | |
console.log(this.foo === "bar") | |
console.log(bar === "bar") | |
} | |
] | |
function runStack(context, stack) { | |
stack.reduce(function (memo, outer) { | |
return function (inner) { | |
memo.call(context, function () { | |
var args = [].slice.call(arguments).concat(inner) | |
outer.apply(context, args) | |
}) | |
} | |
})() | |
} | |
runStack(stack, { foo: "bar" }) | |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment