Created
July 6, 2019 11:29
-
-
Save hijiangtao/8f68f3f0d598a5419f812887f208e5be to your computer and use it in GitHub Desktop.
Brief explanation of redux middleware's work internals
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
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