Skip to content

Instantly share code, notes, and snippets.

@hijiangtao
Created July 6, 2019 11:29
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 hijiangtao/8f68f3f0d598a5419f812887f208e5be to your computer and use it in GitHub Desktop.
Save hijiangtao/8f68f3f0d598a5419f812887f208e5be to your computer and use it in GitHub Desktop.
Brief explanation of redux middleware's work internals
function compose(...funcs) {
if (funcs.length === 0) {
return arg => arg
}
if (funcs.length === 1) {
return funcs[0]
}
return funcs.reduce((a, b) => (...args) => a(b(...args)));
}
const a = (cb) => {
console.log('this is a first cycle');
return () => {
console.log('a result');
return cb();
};
}
const b = (cb) => {
console.log('this is b first cycle');
return () => {
console.log('b result');
return cb();
};
}
const c = (cb) => {
console.log('this is c first cycle');
return () => {
console.log('c result');
return cb();
};
}
console.log('---');
compose(a,b,c)(()=>{})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment