1

I have an application that I used async to avoid the "spaghetti callback" and worked properly, but in certain parallel calls I had to make some changes because the values have to be changed before returning, as these changes made the same routine I thought to make a function to do so and thus save code, but does not work the application (I'm new to javascript and I'm learning).

Debug (console.log), the error in some cases is the same callback that he is called from different requests (if they are local variables do not understand how this happens). I have tried to change the code with forEach and async.each, but in both cases I have errors and no longer is that more change to keep trying, can't find the fault.

My original code (I summarize it to avoid a long post):

async.parallel({
    today: function(callback){

        data.get('DATA', function(err, dataGet){

            if(err){
                callback(err);
            }
            callback(null, dataGet);
        });
    },
    .... yesteday, week, month ....
    year: function(callback){

        data.get('DATA', function(err, dataGet){

            if(err){
                callback(err);
            }
            callback(null, dataGet);
        });
    }
},
function(error, results){
    --- routine ----
});

And my new code is this:

Function

function getDataChange(key, valuePass, callback){

    var values = [ .... ],
        totalData = 0.00;

    /*
    async.each(values, function(value, cb){

        var keyR = key.replace(/%value%/g, value.toLowerCase() );

        data.get(keyR, function(err, dataGet){

            if(err){
                cb(err);
            }

            dataGet = ( dataGet !== null ) ? dataGet : 0 ;

            if( valuePass === value ) {

                totalData += parseFloat( dataGet );
                cb();

            } else {

                valueConverter.convert({ force: true, multi: true }, function(data){

                    totalData += parseFloat( data );
                    cb();
                });
            }
        });
    },
    function(err){

        if(err){
            callback(err);
        } else {
            callback(null, totalData );
        }
    });
    */

    var totals = values.length;

    values.forEach(function(value){ 

        var keyR = key.replace(/%value%/g, value.toLowerCase() );

        data.get(keyR, function(err, dataGet){

            if(err){
                return callback(err);
            }
            dataGet = ( dataGet !== null ) ? dataGet : 0 ;
            total--;

            if( valuePass === value ) {

                totalData += parseFloat( dataGet );

                if( totals === 0 ){     
                    callback(null, totalData);
                }
            } else {

                valueConverter.convert({ force: true, multi: true }, function(data){

                    totalData += parseFloat( data );

                    if( totals === 0 ){
                       callback(null, totalData);
                    }
                });
            }
        });
    });
    //callback(null, totalData);
}

And changes principal routine:

var keyBase = '......',
    value = '.....';

async.parallel({

    today: function(callback){
        /*
        data.get('DATA', function(err, dataGet){

            if(err){
                callback(err);
            }
            callback(null, dataGet);
        });
        */
        getDataChange(keyBase + 'DATAD', value, function(err, returnData){

            if(err){
                callback(err);
            }
            //console.log('## Return Data today');
            callback(null, returnData);
        });
        //callback(null, 0);
    },
    .... yesteday, week, month ....
    year: function(callback){

        getDataChange(keyBase + 'DATAY', value, function(err, returnData){

            if(err){
                callback(err);
            }
            console.log('## Return Data year');
            callback(null, returnData);
        });
    }
},
function(error, results){
    --- routine ----
});

I think I'll have to duplicate the code introducing in parallel async calls, since I am not able to operate.

My errors are varied. In various tests I commented the call to the function and I have established the returned from the callback to 0, in several parallel calls, and I have seen that not running final async.parallel callback function (for example, if I commented all calls except today, yesterday and week). In other cases (establishing all calls as a comment and setting the value back to 0, except at yesterday and week), runs twice week callback. If all calls to the function are commented, except for week and today, it causes an exception showing the message "Error: Callback was already called".

I've been stuck for several days and can not find the error or how to solve this problem :-S

Thanks you.

1
  • If I modify the content of the array values ( var values = [ .... ] ) and containing a single element, it works correctly in async.each version. I do not understand what is happening. Commented Nov 8, 2014 at 23:55

1 Answer 1

2

Fixed !

One of the modules that used a function was called request to a web api, much delayed response.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.