0

I have a function like this :

module.exports.download = function (cb) {
   // Some Code
   cb(); 
}

Is it the same to do this :

module.exports.copyimagefromalbumnext  = function (callback) {
    module.exports.download(callback);
}

or

module.exports.copyimagefromalbumnext  = function (callback) {
    module.exports.download( function () { callback(); } );
}

In advance thanks.

mcbjam
  • 7,344
  • 10
  • 32
  • 40

1 Answers1

1

Is callback the same as function () { callback(); }

No. The second function neither cares about this context, passed arguments, nor the return value of an invocation. You could do

function() { return callback.apply(this, arguments); }

but that's just superfluous. Use the first approach and pass callback itself.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Hi. Thanks for youe help. I'm new in nodejs. Can you give me some link for better understand how context is managed. I don't understand wich context is associate to the callback function. – mcbjam May 13 '14 at 19:09
  • One more precision please. Does callback.apply(this, arguments); attach the callback function to the current object this ? In the first manner ( just callback) the contect is the document where callback was define ? I'm right ? – mcbjam May 13 '14 at 19:38
  • No, the context is always just what the caller decides it to be. – Bergi May 13 '14 at 19:51