A utility for wrapping functions in stacks of middleware