Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created April 23, 2012 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/2470137 to your computer and use it in GitHub Desktop.
Save Raynos/2470137 to your computer and use it in GitHub Desktop.
Flow - Sequential control flow
// 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)
})
}
})()
}
// 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