Skip to content

◷ 发表于: 2025-03-18

◷ 更新于: 2025-03-27

🅆 字数: 0

管道函数

javascript
const pipe = (...fns) => (input) => fns.reduce((chain, func) => func(chain), input);

const pipe = (...functions) => (input) => functions.reduce((chain, func) => func(chain), input);

const pipe = (...functions) => (value) => {
	 return functions.reduce((currentValue, currentFunction) => {
		 return currentFunction(currentValue);
	 }, value);
 };
javascript
export const pipeAsync = (...fns) => (input) => fns.reduce((chain, func) => chain.then(func), Promise.resolve(input));
typescript
export const pipeAsync: any = (...fns: Promise<Function>[]) => (input: any) => fns.reduce((chain: Promise<Function>, func: Function | Promise<Function> | any) => chain.then(func), Promise.resolve(input));

使用说明

javascript
const a = (value) => {
    console.log('a_' + value)
    return 'a_' + value
}
const b = (value) => {
    console.log('b_' + value)
    return 'b_' + value
}
const c = (value) => {
    console.log('c_' + value)
    return 'c_' + value
}
// 调用管道函数
pipe(a,b,c)('rxh')
// 调用函数a
a_rxh
// 调用函数b,并将a的结果传给b
b_a_rxh
// 调用函数c,并将b的结果传给c
c_b_a_rxh
// 最终输出结果
'c_b_a_rxh'

基于 CC BY-NC-SA 4.0 许可发布