简体   繁体   中英

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.

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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM