H
H
Hellas2016-10-02 18:15:55
JavaScript
Hellas, 2016-10-02 18:15:55

What does the following line mean in nodejs script?

callback = callback || function() {};

User.updateLastOnlineTime = function(uid, callback) {
    callback = callback || function() {};
    db.getObjectFields('user:' + uid, ['status', 'lastonline'], function(err, userData) {
      var now = Date.now();
      if (err || userData.status === 'offline' || now - parseInt(userData.lastonline, 10) < 300000) {
        return callback(err);
      }
      User.setUserField(uid, 'lastonline', now, callback);
    });
  };

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max Pushkarev, 2016-10-02
@Hellas

if the callback function is not given as a parameter when calling updateLastOnlineTime, then inside updateLastOnlineTime will be initialized with a new empty function.
but it would be better to check that the callback is really a function, and not another object, for example. something like that:

function isFunction(fn) {
        var obj= {};
        return fn && obj.toString.call(fn) === '[object Function]';
}

callback = isFunction(callback) || function() {};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question