Search code examples
javascriptnode.jscallbackfunction-parameter

Function as a parameter javascript syntax


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.


Solution

  • 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.