I have a problem with async.parallel in node js. Following code ist working perfect
var data = {};
data.task_1= function(callback) {
var res_1 = "hello 1";
callback(null, res_1);
}
data.task_2 = function(callback) {
var res_2 = "hello 2";
callback(null, res_2);
}
async.parallel(data, function(err, results) {
if (err){
console.log(err);
}
console.log(results);
});
The result is: { task_1: 'hello 1', task_2: 'hello 2' }
But if I try to do the task with a function calling data from a database, I become following error: TypeError: callback is not a function and the result { task_2: 'hello 2', task_1: undefined }
I see in the log, that the data is retrieved before the json file is logged.
Here is my code:
var data = {};
data.task_1 = async function(callback) {
var res_1 = await getData("xyz");
console.log(res_1);
callback(null, res_1);
}
data.task_2 = function(callback) {
var res_2 = "hello 2";
callback(null, res_2);
}
async.parallel(data, function(err, results) {
if (err){
console.log(err);
}
console.log(results);
});
What do I miss? Thanks for any help!