gpt4 book ai didi

javascript - 在 Javascript 中嵌套许多函数调用(Unix 管道)的好方法

转载 作者:行者123 更新时间:2023-11-30 07:14:20 25 4
gpt4 key购买 nike

我一直在寻找一种很好地进行嵌套函数调用的方法,以避免出现如下情况:

var result = function1(function2(function3()));

或者类似的东西:

var result = function3();
result = function2(result);
result = function1(result);

类似 Unix 管道的东西会很好:

var result = function3() | function2() | function1();

来源:https://www.npmjs.com/package/babel-plugin-operator-overload

|当然是按位或操作,这个例子是抽象的。

有没有人知道有什么方法可以使用 ES5、ES6 或 ES7 实现这样的效果,而无需转译?

编辑

谢谢 T.J Crowder、torazaburo 和 Bergi,你们都在回答中添加了独特、有用和有趣的信息。

最佳答案

没有辅助函数

我最初认为您的问题是在没有任何辅助函数的情况下执行此操作,但您随后的评论表明情况并非如此。如果辅助函数在范围内,请跳过。

无需添加任何辅助函数,您就可以使用 ES6 promises:

Promise.resolve()
.then(function3)
.then(function2)
.then(function1)
.then(result => {
console.log("result is " + result);
});

不比

var result = function1(function2(function3()));

...但至少被调用的函数是按照它们被调用的顺序列出的,并且 promises 在很多方面都非常灵活。

例如:Live copy on Babel's REPL

function function1(arg) {
console.log("function1 called with " + arg);
return "result1";
}
function function2(arg) {
console.log("function2 called with " + arg);
return "result2";
}
function function3() {
console.log("function3 called");
return "result3";
}

Promise.resolve()
.then(function3)
.then(function2)
.then(function1)
.then(result => {
console.log("result is " + result);
});

输出:

function3 calledfunction2 called with result3function1 called with result2result is result1

With a helper function

Re your comment:

function pipe(){
var str = 'Promise.resolve()';
for(var i = 0; i < arguments.length; i++){
str += '.then(arguments[' + i + '])'
}
eval(str);
}

pipe(c, b, a, result => { console.log("result is " + result); });

I know pipe is a thing in fs libraries, so the function name isn't exactly great. Aside from that, is there anything glaringly wrong with this?

If you want to throw a helper function at this, there's no need at all for eval. For non-promise-ified functions, just do:

function pipe(first, ...more) {
return more.reduce((r, f) => f(r), first());
}

let result = pipe(function3, function2, function1);

Live copy on Babel's REPL

如果您想使用 promise 化函数或混合函数来执行此操作,则:

function pipe(...functions) {
return functions.reduce((p, f) => p.then(f), Promise.resolve());
}

然后你可以按照你展示的方式调用它:

pipe(function3, function2, function1, result => {
// ...
});

...但这样做会忽略错误。由于 pipe 返回最后一个 promise,您可以使用所有 promise goodness

pipe(function3, function2, function1, result => {
// ...
}).catch(failure => {
// ...
});

pipe(function3, function2, function1)
.then(result => {
// ...
})
.catch(failure => {
// ...
});

这是一个混合简单函数和返回 promise 的函数的完整示例:Live copy on Babel's REPL

function pipe(...functions) {
return functions.reduce((p, f) => p.then(f), Promise.resolve());
}
function function1(arg) {
console.log("function1 called with " + arg);
return "result1";
}
function function2(arg) {
console.log("function2 called with " + arg);
return new Promise(resolve => {
setTimeout(() => {
resolve("result2");
}, 100);
});
}
function function3() {
console.log("function3 called");
return "result3";
}

pipe(function3, function2, function1)
.then(result => {
console.log("Final result is " + result);
})
.catch(failure => {
console.log("Failed with " + failure);
});

输出:

function3 calledfunction2 called with result3function1 called with result2Final result is result1

关于javascript - 在 Javascript 中嵌套许多函数调用(Unix 管道)的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33591859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com