mikemaccana mikemaccana - 9 months ago 131
Javascript Question

What does someFunction(args)(moreArgs); do in JavaScript?

What does:

someFunction(args)(moreArgs);


Do in JavaScript?

I have a following working piece of code from the Passport.js documentation:

app.get('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) { return next(err); }
if (!user) { return res.redirect('/login'); }
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/users/' + user.username);
});
})(req, res, next);
});


In this case
passport.authenticate
is being called with args (the String
local
and a Function) then afterwards
(req, res, next)
exists.

In otherwords:

someFunction(args)(moreArgs);


This isn't an IIFE - there's no
()
around the function being ran. What is it?

Answer Source

someFunction(args) returns a function, so (moreArgs) is just calling that returned function.

E.g:

function foo(a) {
    console.log(a);
    function bar(b,c){
        console.log(b, c);
    }
    return bar;
}

foo(20)("Thou shalt", "not steal");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download