提问人:Julius Evans 提问时间:4/20/2023 最后编辑:Julius Evans 更新时间:9/10/2023 访问量:62
如何使用回调的结果并将该结果传递给下一个回调?
How do I use the result of a callback and pass that result to the next callback?
问:
所以我的函数接受一个值和任意数量的回调作为 参数(我应该使用扩展运算符吗?该函数应返回通过所有给定回调传递值的最终结果。
let chainMap = function(num, cb1, cb2, cb3) {
if(cb2 === undefined && cb3 === undefined){
let res1 = cb1(num);
return res1;
} else if(cb3 === undefined){
let res2 = cb2(res1);
return res2;
} else {
let res3 = cb3(res2);
return res3;
}
};
let add5 = function(n) {
return n + 5;
};
let half = function(n) {
return n / 2;
};
let square = function(n) {
return n * n;
};
console.log(chainMap(25, add5)); // 30
console.log(chainMap(25, add5, half)); // 15
console.log(chainMap(25, add5, half, square)); // 225
console.log(chainMap(4, square, half)); // 8
console.log(chainMap(4, half, square)); // 4
我返回的“cb2(res1)”不是一个函数。如何将第一次回调的结果传递给下一个回调?
答:
0赞
Rayken
4/21/2023
#1
res1 在此作用域中声明为局部变量,不能在作用域外使用它。
if(cb2 === undefined && cb3 === undefined){
let res1 = cb1(num);
return res1;
}
此外,您不能使用 和 if/else 语句“链接”。 下面是你尝试执行的操作的示例代码:
let chainMap = function(num, cb1, cb2, cb3) {
let temp = 0;
if(cb1){
temp = cb1(num);
}
if(cb2){
temp = cb2(temp)
}
if(cb3){
temp = cb3(temp);
}
return temp
};
let add5 = function(n) {
return n + 5;
};
let half = function(n) {
return n / 2;
};
let square = function(n) {
return n * n;
};
console.log(chainMap(25, add5)); // 30
console.log(chainMap(25, add5, half)); // 15
console.log(chainMap(25, add5, half, square)); // 225
console.log(chainMap(4, square, half)); // 8
console.log(chainMap(4, half, square)); // 4
另一种方式:
let chainMap = function(num, cb1, cb2, cb3) {
if(cb2 === undefined && cb3 === undefined){
let res1 = cb1(num);
return res1;
} else if(cb3 === undefined){
let res2 = cb2(cb1(num));
return res2;
} else {
let res3 = cb3(cb2(cb1(num)));
return res3
}
};
let add5 = function(n) {
return n + 5;
};
let half = function(n) {
return n / 2;
};
let square = function(n) {
return n * n;
};
console.log(chainMap(25, add5)); // 30
console.log(chainMap(25, add5, half)); // 15
console.log(chainMap(25, add5, half, square)); // 225
console.log(chainMap(4, square, half)); // 8
console.log(chainMap(4, half, square)); // 4
0赞
kr3nk1
9/10/2023
#2
另一种方法是在回调函数数量不确定的情况下使用 rest 参数语法:
let chainMap = function(num, ...callbacks) {
let result = num;
if(callbacks === undefined) {
return result;
} else {
callbacks.forEach(element => {
result = element(result);
});
return result;
}
};
let add5 = function(n) {
return n + 5;
};
let half = function(n) {
return n / 2;
};
let square = function(n) {
return n * n;
};
console.log(chainMap(25, add5)); // 30
console.log(chainMap(25, add5, half)); // 15
console.log(chainMap(25, add5, half, square)); // 225
console.log(chainMap(4, square, half)); // 8
console.log(chainMap(4, half, square)); // 4
评论
undefined
Array.reduce
let chainMap = function(num, cbs) { return cbs.reduce((a, cb) => cb(a), num); }
cb2 && cb3 === undefined)
<--这并不像你认为的那样。首先,检查一个真实值,然后(如果是)检查它是否完全是 。如果你想同时检查两者,你可以写cb2
cb3
undefined
undefined
cb2 === undefined && cb3 === undefined)