Problem with variable and self-execution anonymous function

26 views
Skip to first unread message

Kleyson Rios

unread,
Sep 24, 2014, 7:46:27 AM9/24/14
to nod...@googlegroups.com
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.


Kleyson Rios

unread,
Sep 24, 2014, 1:45:04 PM9/24/14
to nod...@googlegroups.com
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
();
   
});
Reply all
Reply to author
Forward
0 new messages