1

I have been developing in PHP for many years and now I´m trying to learn NodeJS, and I´m having problems with asynchronous functions...

I have this function in NodeJS (source is an array of users):

//userformat.js

var age = module.exports.age = function(date){

  var diff = new Date - new Date(date);
  var diffdays = diff / 1000 / (60 * 60 * 24);
  var age = Math.floor(diffdays / 365.25);

  return age;

}

...etc.

//index.js

function format(source, me, callback){

    var len = source.length

    for (var i = 0; i < len; i++){

        source[i]['_age'] = userFormat.age(source[i]['birthday']);
        source[i]['_rating'] = userFormat.rating(source[i]['votes']);
        source[i]['_is_followed'] = userFormat.followed(source[i]['fllws'], me);

    }

    callback(null, source);

}

I know that function is wrong in Node, because is possible that the function calls the callback before the functions in the for() had finished, and the values in the array could be undefined, for this reason I changed the function using callbacks:

//userformat.js

var age = module.exports.age = function(date, callback){

      var diff = new Date - new Date(date);
      var diffdays = diff / 1000 / (60 * 60 * 24);
      var age = Math.floor(diffdays / 365.25);

      callback(age);

    }
...etc.

//index.js

function format(source, me, callback){

    var len = source.length

    for (var i = 0; i < len; i++){

        userFormat.age(source[i]['birthday'], function(resul){

            source[i]['_age'] = resul;

            userFormat.rating(source[i]['votes'], function(resul){

                source[i]['_rating'] = resul;

                userFormat.followed(source[i]['fllws'], me, function(resul){

                    source[i]['_is_followed'] = resul;

                    //Callback
                    if (i == len-1){
                        callback(null, source);
                    }

                })

            })


        })

    }

}

That is correct? I have another way to do it more efficient or elegant?

Thank you so much!

Miguel Haba
  • 57
  • 1
  • 6

1 Answers1

0

I think you misunderstood the concept of asynchronous in nodeJS. Your code isn't asynchronous and your callback will always be called after the loop.

Check this question : how do i create a non-blocking asynchronous function in node.js?

Community
  • 1
  • 1
arthur.flachs
  • 136
  • 1
  • 8
  • Yes.. I know that the callback is called after the loop, but the vars inside the loop are setting asynchronously. userFormat.age(), userFormat.rating() and userFormat.followed() are functions that returns a value. – Miguel Haba Feb 22 '15 at 19:34
  • Could you show what the userFormat methods are doing exactly ? – arthur.flachs Feb 22 '15 at 19:38
  • I have edit my first question... I´m sorry, asynchronous methods are very confused for me... – Miguel Haba Feb 22 '15 at 19:47
  • Your methods are not asynchronous (at least the age method). Read the explanations from the link I gave you, it should help you understand it. If you really need your methods to be run asynchronously I think you should use the process object (http://nodejs.org/api/process.html), and the nextTick method. – arthur.flachs Feb 22 '15 at 20:14
  • Thank you Arthur, I have to study more :) . I´ll read the links, are very interesting – Miguel Haba Feb 22 '15 at 21:52