0

I am trying to use the async utility for the node.js. Below is my code. The console prints "In my func1" and "In my func2". I also expect it to print "call back func" but it won'nt

var myfunc1 = function(callback) {
var a = 1;
console.log ("In my func1");
};
var myfunc2 = function(callback) {
var b = 2;
console.log ("In my func2");
};

models.Picture.prototype.relativeSort = function(viewer_location) {
console.log("Rel Sort");

var sortedPics = [];
var relsortedPics = [];

 // Finds all the pictures
 async.series([myfunc1(), myfunc2()],
 function(err, results){
    if(err) {
        throw err;
    }
 console.log("call back func");
    return a+b;
    });
 };
mu_sa
  • 2,685
  • 10
  • 39
  • 58

1 Answers1

2

You need to use the callback argument, for example:

var myfunc1 = function(callback) {
    var a = 1;
    console.log ("In my func1");
    callback(null, 'test');
};

The first argument of callback is an error, and the second the result you want to pass to the final handler.

EDIT

I've missed the other mistake. This line:

async.series([myfunc1(), myfunc2()],

should be

async.series([myfunc1, myfunc2],

You are not supposed to call functions. You are telling async: "Hey, take these functions and do something (asynchronous) with them!".

freakish
  • 54,167
  • 9
  • 132
  • 169
  • It does not work...On the line callback (null, 'test') it has an error 500 TypeError: undefined is not a function – mu_sa Jun 25 '12 at 12:54