Answer the question
In order to leave comments, you need to log in
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
});
};
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"
}];
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.
});
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);
});
};
[ [], [] ]
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'
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question