С вашим кодом это возможно, но при обратном вызове функции вы хотите передать каждую переменную, к которой вы можете обратиться, в функцию.Это как отображать, фильтровать и сокращать работу.
function forWithAction(r,callback){
for(let i = 0; i < r; i++){
callback(i, r);
}
}
//iVal and rVal are just place holders for i and r that I'm passing into this function
forWithAction(5,function(iVal, rVal){
console.log(iVal);
});
Пример с картой
function Map(arr, callback)
{
let output = [];
let secretVariable = 'I\'m hidden inside :D';
for(let i = 0; i < arr.length; i++)
{
//I'm passing 4 values into my callback. So any call back I create
//I can access these 4 values
let val = callback(arr[i], i, arr, secretVariable)
output.push(val);
}
return output;
}
let myArray = [2, 5, 1, 8, 9, 3, 2];
Map(myArray, function(val, ind, arr, secret) {
console.log('__ARRAY_VALUE__: ', val);
console.log('__INDEX__(i) :', ind);
console.log(secret);
return val;
});