【发布时间】:2018-12-21 12:56:45
【问题描述】:
如果标题没有意义,真的很抱歉。不知道如何让我的问题简短
我想知道的是,
我有一个recursive function,好吧不一定是recursive function,只是我在做这个功能的时候,我想知道它是否可以以更灵活的方式重用。
我的函数看起来像这样runAxios(ele, api)是我想知道是否可以重复使用的函数
const ct = (arr, num, res) => {
const promises = [];
for(let i = 0; i < num; i++){
const ele = arr.shift(); // take out 1st array, literally take out
if(ele){
promises.push(
runAxios(ele, api) // this is the function I am wondering if can be reused
)
}
}
Promise.all.......
};
如果runAxios(ele, api) 可以是任何东西,那么我相信这个ct 可以更灵活?
我想知道它是否可以像这样
const ct = (arr, num, res, fx) => {
const promises = [];
for loop......
if(ele){
promises.push(
fx // this is then passed as any other function other than just a fixed `axios function` that I wrote
)
}
}
Promise.all........
};
当我第一次尝试时,我意识到这是行不通的,因为runAxios 的第一个参数是在循环内完成的,这意味着该变量在进入函数本身之前还不存在。
只是好奇是否有这样一种方法可以轻松做到,我只是不知道如何或实际上不可能?
提前感谢您的任何建议。
【问题讨论】:
标签: javascript function parameters parameter-passing