Using Lambdas with Bluebird.Map

A situation came up this week where a coworker had stumbled upon some really terse code. They weren’t exactly clear on what was going on, so we went through an exercise together of rewriting the code in the most verbose format, then gradually shrinking it one piece at a time to arrive at the very terse but completely equivalent format.

Our Task: Put Users into Groups

Here’s the situation. We need to do a series of database inserts to add a user to multiple groups. This is done with an SQL INSERT statement for each groupId linking that group to the given user.

Here’s the initial setup code that will be unchanged for all the examples in this post:

WTF is this?

The initial code encountered, which was unclear, was

This makes use of Function.prototype.bind which is available on all functions. To show what’s going in in this very terse bit of code, we coded up the fully verbose format of equivalent code as follows.

We’re using to iterate over each groupId in the groupIds array so the perGroup function will be invoked 4 times since there are 4 values in the groupIds array. Each invocation calls addToGroup with the arguments in the proper order, and returns the promise that addToGroup returns.

addToGroup does the DB insert against our mocked-up database library which has been promisified with the Bluebird.promisifyAll() utility function so insertAsync will return a bluebird promise and not need a callback even though insert is coded as a callback API.

When coded like that, it was clearer what was going on. So we then started to trim out syntax that is technically unnecessary step by step as a learning exercise.

Shrinking it down

First to go was just the name of the perGroup function, making it a lambda (anonymous function).

Sprinkle Some ES2015 Arrow Function On It

Next we opted for the terser ES2015 arrow function syntax.

Lose the Curly Braces

Now what we notice is we have an arrow function that just returns the value of a single expression. This means it’s eligible for the ultra-terse no-curly form. So we axe the curlies and the return keyword and keep it on one line.

OK now that’s pretty terse. However, look closely at the anonymous mapping function itself (groupId) => addToGroup(userId, groupId). How different is it from addToGroup? Well, instead of taking 2 arguments, it only takes one. Then it calls addToGroup with 2 arguments, the first being the userId which is directly in the parent scope and the second being the groupId that comes from So you could think of it like a variant of addToGroup with the userId argument hard coded or “baked in” to be a particular value.

It turns out that this is exactly what Function.prototype.bind does. Given a function, bind creates a new function that has some of the arguments pre-specified (“bound”) to particular values, leaving only the remaining arguments to be varied on each function call.

Back to .bind

So that gets us back to the .bind format.

It’s a few characters shorter than even the tersest arrow function.

What we are doing here is creating a new, unnamed function with pre-specified this value and first argument value. When we call addToGroup.bind(null, userId) passing null as the first argument means the function bind returns will not have access to a this variable when called. addToGroup is a regular function (not an object-oriented method) and makes no reference to this, so it’s perfectly OK to have it be null.

The second argument to bind will be “bound” as the first argument of our new addToGroup variant. That leaves the 2nd argument unspecified, which lines up with the expecting a function that takes 1 argument which is a groupId. Remember that bind returns the new function without executing it. So you could express this in English as “Hey addToGroup function, build me a variant of yourself with this userId fixed as the first argument and the other argument unspecified until later when I call the function”.

lodash partial version

Another option if that ugly null in the .bind call rubs you the wrong way would be lodash’s _.partial which looks like this:

Lessons Learned

My take is that the implicit-return arrow function flavor:, (groupId) => addToGroup(userId, groupId)) is the tersest you can go and still be readable to developers without much exposure to .bind, which is a fairly advanced feature in regular JS codebases, although in functional programming it implements the idea of partial application, which is very commonplace and not advanced for developers accustomed to the functional programming paradigm.

I think explicitly seeing that you have a function that takes a single groupId argument and gets userId from the parent scope is important for this to be clear, and with .bind you lose that and it becomes magic.

The main takeaway is that all formats of this code are exactly equivalent. It’s good to be able to read and understand every variation so you are prepared if you encounter it in your coding.

Thanks to Scott Nonnenberg for reviewing a draft of this post!

Gorilla Logic is always on the hunt for the best software engineers. Think you have what it takes to be a Gorilla? Check out our career section by clicking below 


Using Lambdas in Bluebird.Map 

Pete Lyons
Pete Lyons has been doing professional software development since 2001. His experience includes the full spectrum of businesses from large Fortune-500 Enterprises through growing mid-sized companies, down to brand new startups with a handful of founders. He has developed web-based business applications of many flavors, enterprise data center management software, social media applications, industrial control systems, and back end microservices. Pete hopped on the node.js train in the early days of 2011 and has been focusing on node.js expertise primarily since then. He has helped many teams and companies succeed with node.js in his consultancy, answered over 1500 stackoverflow questions, presented at meetups, and written dozens of blog posts. Pete likes the pragmatic craftsman approach to software development and enjoys teaching beginners and pros alike. Away from the computer, Pete performs regularly on saxophone with his 10-piece brass band Gora Gora Orkestar and enjoys rock climbing.

Deliver off-the-chart results.

WordPress Video Lightbox Plugin