javascriptnode.jsflow-controlnimble

Using a common variable in Node.js Nimble flow.parallel control


I have the following code:

var flow = require('nimble');

exports.execute = function(data, estrategias, callback) {

    var estrategiaList = [];
    var resultList = [];

    for (var i in estrategias)
    {
            var estrategia = (function(item) {

                    return function(callbackFlow) {

                            // Cria uma nova instancia do indicador
                            var indicador = loadIndicador(item);

                            // Executa indicador
                            indicador.execute(item, data.fechamento, function(result) {
                                    resultList.push(result);
                            });

                            callbackFlow();
                    }

            })(estrategias[i]);

            estrategiaList.push(estrategia);
    }

    flow.parallel(estrategiaList, function() {
            callback(resultList);
    });
};

For some reason on the final flow.parallel callback callback(resultList) I'm getting the resultList empty.

I've checked that resultList.push(result) add successfully the result in the resultList array.

I think the problem is some small detail that I couldn't figured out.

Can someone help me ?

Regards. Kleyson Rios.


Solution

  • I figured out the problem. callbackFlow() was being executed before than resultList.push(), so I had to put callbackFlow() inside of indicador.execute as:

    indicador.execute(item, data.fechamento, function(result) {
        resultList.push(result);
        callbackFlow();
    });