Firstly, this is not about chaining. I understand chaining is around function methods and this isn't, and I haven't been able to find a term for this.
Basically what I'm looking for is a clean way to pass output of one function into another and chain it. Something that looks like this
const updatedAssets = addCategory(addTagLine(addTags(addImages(addPoints(addID(addTitle(assets)))))));
Basically assets
is an object and each function will add new keys and values into it.
If you simply want to avoid the nesting and improve readability, you can make an array of your functions and use reduce()
/ reduceRight()
to compose them. For example:
let one = n => "one " + n let two = n => "two " + n let three = n => "three " + n let functions = [one, two, three] let res = functions.reduce((res, f) => f(res), "go") console.log(res) // or the opposite direction: let resRev = functions.reduceRight((res, f) => f(res), "go") console.log(resRev)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.