Skip to content

Instantly share code, notes, and snippets.

@ssnau
Created November 30, 2015 08:12
Show Gist options
  • Save ssnau/8f29e620cf7816d50618 to your computer and use it in GitHub Desktop.
Save ssnau/8f29e620cf7816d50618 to your computer and use it in GitHub Desktop.
function m1(next) {
console.log('1: start');
next();
console.log("1: end")
}
function m2(next) {
console.log('2: start');
next();
console.log('2: end');
}
function m3(next) {
console.log('3: start');
next();
console.log('3: end');
}
var middleware = compose([m1, m2, m3]);
middleware();
function noop(){}
function compose(middlewares) {
return function middleware() {
var stack = middlewares.slice();
function next() {
var handler = stack.shift();
if (!handler) return;
handler(next);
}
next();
return next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment