0

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?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • 2
    Thats called currying. Thats just a function returning a function. – Jonas Wilms Nov 24 '17 at 15:45
  • @JonasW. This isn't a duplicate. The question is different, and the answer is different, although they're clearly related. – mikemaccana Nov 24 '17 at 15:47
  • 1
    You asked *what is it* , the answer is *its currying*. Thats it. – Jonas Wilms Nov 24 '17 at 15:50
  • @JonasW. Yes. I asked 'What does someFunction(args)(moreArgs); do in JavaScript?'. This is a different question from 'What is Currying?'. To compare them, try reading the questions and noting that they aren't the same. Then read the answer and note that isn't the same either. – mikemaccana Nov 24 '17 at 15:53
  • Why do you care? I hope the duplicate answers everything you need to know, so you achieved your aim dont you? – Jonas Wilms Nov 24 '17 at 15:58
  • 1
    @JonasW. Did we ever have a conversation where I argued the same things OP is arguing ? Or was that Bergi ? he said that someone who searches for "what does somefunction...." and finds it to be a duplicate will just have to do one extra click to get to his answer, I was convinced then, but I have a different perspective now, StackOverflow should have a mechanism for adding multiple titles to questions (a beginner is more likely to search with something similar to OP's title, if one knows about the term "currying", they are almost guaranteed to know what it does as well). – doubleOrt Nov 24 '17 at 16:01
  • My point is that all these extra clicks could amount to millions of wasted seconds. – doubleOrt Nov 24 '17 at 16:03
  • @JonasW. Two points here: 1. there's obviously not duplicates as the question and the answer are substantially diffeent. 2. Exactly as Taurus pointed out: I didn't know what currying was when I asked the question. So I wouldn't have searched for this unknown concept. Nor would I expect an answer answering that question, since that isn't what I asked. – mikemaccana Nov 24 '17 at 16:10
  • @JonasW. Also 'That's called currying' is better written as an answer than a comment (perhaps with the link to the question whose answer covers that topic). – mikemaccana Nov 24 '17 at 16:12

2 Answers2

4

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");
Matthew Spence
  • 986
  • 2
  • 9
  • 27
doubleOrt
  • 2,407
  • 1
  • 14
  • 34
1

Thats called currying. Thats just a function returning a function.

In js that can be written easily using arrow functions:

const curry = arg1 => arg2 => console.log(arg1, arg2);

curry(1)(2)
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151