0

I'm running this simple code as a test for a function I need to make for my project:

var async = require('async');

test_function = function(a,b,callback){
    console.log(a,b);
    callback(null,a+b)  
};

var array = [12,7,3,4,2];
var waterfall_array = [];

waterfall_array.push(function(callback_waterfall){
    test_function(array[0],3,callback_waterfall)
});

for(var i=1; i<array.length; i++){
    waterfall_array.push(function(arg,callback_waterfall){
        var array_bis = array ;
        console.log(array_bis,array_bis[1]);
        test_function(array_bis[i],arg,callback_waterfall);
    });
}

async.waterfall(waterfall_array,function(error,result){
    if(error){
        console.log('error');
    }
    else{
        console.log(result);
    }
});

Here is what I get in my console :

12 3
[ 12, 7, 3, 4, 2 ] 7
undefined 15
[ 12, 7, 3, 4, 2 ] 7
undefined NaN
[ 12, 7, 3, 4, 2 ] 7
undefined NaN
[ 12, 7, 3, 4, 2 ] 7
undefined NaN
NaN

Why can't I give the array_bis[i] as an argument in my waterfall function?

Looking at the answer on another toipic I've tried this :

var async = require('async');

test_function = function(a,b,callback){
    console.log(a,b);
    callback(null,a+b)  
};

var array = [12,7,3,4,2];
var waterfall_array = [];

waterfall_array.push(function(callback_waterfall){
    test_function(array[0],3,callback_waterfall)
});

functioncreate = function(array_index){
    return function(arg_waterfall,callback){
        test_function(array[array_index],arg_waterfall,callback);
    };
}

var function_array = [];
for (var j=1;j<array.length;j++){
    function_array.push(functioncreate(j));
}

console.log(function_array);

for(var i=1; i<array.length; i++){
    waterfall_array.push(function(arg,callback_waterfall){
        function_array[i](arg,callback_waterfall);
    });
}

async.waterfall(waterfall_array,function(error,result){
    if(error){
        console.log('error');
    }
    else{
        console.log(result);
    }
});

but I'm returned the error : TypeError: function_array[i] is not a function

Which I don't understand since function_array[i] should be a function returned by functioncreate.

Do you have any tips for this ?

Thank you :)

Ok so this is the right code :

var async = require('async');

test_function = function(a,b,callback){
    console.log(a,b);
    callback(null,a+b)  
};

var array = [12,7,3,4,2];
var waterfall_array = [];

waterfall_array.push(function(callback_waterfall){
    test_function(array[0],3,callback_waterfall)
});

functioncreate = function(array_index,arg_waterfall,callback){
    return (function(arg_waterfall,callback){
        test_function(array[array_index],arg_waterfall,callback);
    });
}

var function_array = [];
for (var j=1;j<array.length;j++){
    waterfall_array.push(functioncreate(j));
}

console.log(function_array);

async.waterfall(waterfall_array,function(error,result){
    if(error){
        console.log('error');
    }
    else{
        console.log(result);
    }
});

Thank you !

Stéphane Bnn
  • 75
  • 2
  • 11
  • "function_array[i] should be a function returned by functioncreate" — It is, until `i` is the same as `function_array.length` (when it is `undefined`), which it will be by the time any of the callback functions get called. **You are still using `i` from the `for` loop *inside* the callback function you pass**. – Quentin Dec 15 '15 at 11:13
  • Yes thank you for your help ! – Stéphane Bnn Dec 15 '15 at 11:18

0 Answers0