Amol M Kulkarni
Amol M Kulkarni

Reputation: 21629

Correct way to return a value from Node.js function

Consider the following functions where I am returning a value.

Method 1:

function getData() {
    var myData = '';
    var DateObj = new Date();
    for (i=0; i<50000; i++) {
         myData += "'" + DateObj.toString() +"', ";
    }

    return myData;
}

Method 2:

function getData(calbck) {
  try{
    var myData = '';
    var DateObj = new Date();
    for (i=0; i<50000; i++) {
         myData += "'" + DateObj.toString() +"', ";
    }
  } catch(e){
  return calbck(e,null);
  } 
  return calbck(null, myData);
}

I have tested both the ways works. Which on is the best to choose?

Thanks


Update: Tested with the following code.

function getData1() {
    var myData = '';
    var DateObj = new Date();
    for (i=0; i<50000; i++) {
         myData += "'" + DateObj.toString() +"', ";
    }
    return myData;
}

function getData2(calbck) {
  try{
    var myData = '';
    var DateObj = new Date();
    for (i=0; i<50000; i++) {
         myData += "'" + DateObj.toString() +"', ";
    }
  } catch(e){
  return calbck(e,null);
  } 
  return calbck(null, myData);
}

console.log('call getData1() start');
console.time('5000000-getData1()');
getData1()
console.log('call getData1() End');
console.timeEnd('5000000-getData1()');

console.log('call getData2(calbck) start');
console.time('5000000-getData2(calbck)');
getData2(function(err,Returned_value){
if(err) return console.log(err);
   //console.log(Returned_value);
   console.log('call getData2(calbck) End');
   console.timeEnd('5000000-getData2(calbck)');
});

Output:

D:\Test>node app.js
call getData1() start
call getData1() End
5000000-getData1(): 91ms
call getData2(calbck) start
call getData2(calbck) End
5000000-getData2(calbck): 76ms

D:\Test>node app.js
call getData1() start
call getData1() End
5000000-getData1(): 111ms
call getData2(calbck) start
call getData2(calbck) End
5000000-getData2(calbck): 78ms

Upvotes: 2

Views: 258

Answers (2)

Myrne Stol
Myrne Stol

Reputation: 11438

There's good reason to go use the "asynchronous" function signature, even if its current implementation is synchronous. A function named getData (and especially its implementation, which screams "stub" to me) suggests that this function may be doing I/O (network, file system) in the future. If you give the getData function an asynchronous function signature now, you're forced to make all code using getData pass a callback. And that may be a a good thing!

Changing the function signature of such a method later on can have ramifications for all code based using getData, be it directly or indirectly.

If the question would be about a hypothetical computeSum function, and what method signature to give that, I'd definitely recommend to use a synchronous function signature. But now, it's not so clear.

In general, I'd give any function I write an asynchronous signature (or have it return a promise) if I see a good chance that its implementation will do I/O in the future, even if its current implementation does not.

Upvotes: 2

Kendall Frey
Kendall Frey

Reputation: 44316

It all comes down to whether your function is synchronous or asynchronous.

If your function is synchronous, and all the calculation is done immediately, use the first method.

If your function asynchronous, and it uses resources such as disk or network I/O, or ends up waiting on anything, then the second method is really the only option.

In your example, the function is synchronous, so you should use the first one.

Upvotes: 3

Related Questions