T
T
Tolych2015-10-08 13:15:03
Node.js
Tolych, 2015-10-08 13:15:03

How to organize parallel execution of code?

Good afternoon!
I can't understand why the result is not returned:
Here are the sources:

var async = require('async'); // подключаем библиотеку

var procedure = function(mon, tue, wed, thu, fri, sat, sun, buksumm, ID, sql, callback){

    var conn = new sqlDb.Connection(settings.dbConfig);
    conn.connect()
    .then(function(){
        var req = new sqlDb.Request(conn);
        //req.verbose = true;
        req.input('mon', mon);
        req.input('tue', tue);
        req.input('wed', wed);
        req.input('thu', thu);
        req.input('fri', fri);
        req.input('sat', sat);
        req.input('sun', sun);
        req.input('BUKING', buksumm);
        req.input('IDCity', ID);
        req.execute(sql)
        .then(function(data){
          conn.close();
          callback(null, data[0]); // use CPS, pass error as first argument, if any
        })
        .catch(function(err){
          //console.log(err);
          conn.close();
          callback(err, null); // use CPS, pass error as first argument, if any
        });
    })
    .catch(function(err){
        //console.log(err);
        conn.close();
        callback(err, null); // use CPS, pass error as first argument, if any
    });
};

In the procedure variable, I drive a function that executes a stored procedure, depending on the parameters, and returns the result.
Since the nth number of points can be selected on the client form, the request will come to us in the form of an array of data. For example, this if two points are selected:
var testArray = [{
  "mon" : 1,
  "tue" : 1,
  "wed" : 1,
  "thu" : 1,
  "fri" : 1,
  "sat" : 1,
  "sun" : 1,
  "buksumm": 1000,
  "ID": "FBAF60C0-7330-4B93-A944-048FCB74DA2C"
},{
  "mon" : 2,
  "tue" : 2,
  "wed" : 2,
  "thu" : 2,
  "fri" : 2,
  "sat" : 2,
  "sun" : 2,
  "buksumm": 2000,
  "ID": "8D264BAC-19C5-429A-B11F-D5891409F81E"
}];

Next, I want to execute the stored procedure N times. Where N = number of selected points. To do this, I use the async library, the parallel method.
Here is an example of its execution code:
async.parallel([
    function(callback){
        setTimeout(function(){
            callback(null, 'one');
        }, 200);
    },
    function(callback){
        setTimeout(function(){
            callback(null, 'two');
        }, 100);
    }
],
// optional callback
function(err, results){
    // the results array will equal ['one','two'] even though
    // the second function had a shorter timeout.
});

Since we do not know in advance how many points will be selected, I create an array of functions. Which will be passed as a parameter to async.
var arrayFunctions = [];
function getResults(array){//на входе принимает наш массив точек
  for (var i = 0; i < array.length; i++) {
    var setFunction = function(cb){
      var sql = "getByCityes"; //определяем наименование вызываемой хранимой процедуру
      procedure(array.mon, array.tue, array.wed, array.thu, array.fri, array.sat, array.sun, array.buksumm, array.ID, sql, cb);
    };
    arrayFunctions.push(setFunction);
  }
  async.parallel(arrayFunctions, function(err, results){
    if(err) console.log(err);
    console.log(results);
  });
};

The result, unfortunately, is returned like this:
[ [], [] ]
Although, if you execute the procedures in mssql:
EXECUTE getByCityes 2,2,2,2,2,2,2,2000,'8D264BAC-19C5-429A-B11F-D5891409F81E'
EXECUTE getByCityes 1,1,1,1,1,1,1,1000,'FBAF60C0-7330-4B93-A944-048FCB74DA2C'

The result is returned.
At first glance, maybe I'm doing something wrong? And it was not worth it to generate an array of functions in this way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Shatokhin, 2015-10-08
@Tolych

Wouldn't it be easier to use async.map?

var sql = "getByCityes";
async.map(array, function (item, cb) { 
  procedure(item.mon, item.tue, item.wed, item.thu, item.fri, item.sat, item.sun, item.buksumm, item.ID, sql, cb);
}, function(err, results){
    if(err) console.log(err);
    console.log(results);
  });
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question