Bluebird’s map and mapSeries explained by Cooking Pasta

Keyur Patel
Feb 23, 2017 · 4 min read

What are Promises in JavaScript? How do they work? This is one of the common questions being asked for JavaScript positions. Someone asked me that and I couldn’t articulate it very well. So, I decided to take it upon myself to write a blogpost about it and understand the concept in detail. A simple definition of Promise is that it is a placeholder for a value that would be resolved sometime in the future. It can be in any of these three states: pending, fulfilled or rejected.

Now, let’s use the analogy of making pasta to illustrate Promises and hopefully you will be able to understand them better. I am going to make vegetarian penne pasta with red sauce and add some broccoli. Let’s look at all the ingredients we need:

  1. Penne pasta
  2. Broccoli
  3. Red sauce
  4. Mozzarella cheese

In order to complete the dish, I need to:

  1. Boil pasta in water (5 mins)
  2. Cut broccoli into medium size pieces ( 3 mins)
  3. Pre-heat the oven to 400 degrees Fahrenheit ( 3 mins)
  4. Lay the boiled pasta in rectangular glass pan ( 1 min)
  5. Put a layer of red sauce, broccoli and cheese ( 1 min)
  6. After the oven is pre-heated, put the pasta pan in the oven ( 1 min)
  7. Pasta is ready to be served after 15 mins (15 mins)

Before we make our pasta in JavaScript, let’s understand how JavaScript executes the code. It will execute the code synchronously. We know that it takes 5 mins to boil the pasta, 3 mins to cut broccoli, and preheat the oven… In order to simulate that, we have to put a delay after the start of each operation. We can do this by using JavaScript’s “setTimeout” function and passing in the delay amount in milliseconds. For the sake of simplicity in this example, we will consider 1 millisecond = 1 min.

Using native Promises, we can ensure that one operation is completed before the next one is set to execute. Here is the code for making pasta assuming we complete one operation at a time.

Assuming we have already written the functions to perform the steps, “pasta making code” could be written much succinctly by using Bluebird, a javascript promise library. Bluebird’s map function allows us to make promise calls in an array and, only after all of them are resolved, it returns the result in an array format. The map function will start all the operations at once in parallel, and depending on when all of them are resolved it will return the result. In the example below, I am using mapSeries instead of map because, when making pasta, I cannot be doing certain operations such as boiling the pasta and putting it in the pan at the same time. Therefore, I have decided to do all the operations in series using bluebird’s mapSeries to iterate through each of the steps one at a time.

We can also do the same thing with the map function if we give its concurrency property a value of 1. The only difference is mapSeries will perform each operation in series whereas map, with a concurrency of 1, will pick any operation randomly. Both will only perform one operation at a time. Using this method, the time it took for pasta to be ready is 29 minutes (added up all the times for each step in series)

In the above method, we are only performing one step at a time, i.e. only starting the subsequent task after the previous one is complete. We can cut down the time it takes to make pasta by performing some of the initial steps in parallel, like starting step 1,2 & 3 at the same time. Only after they are finished, we can perform the rest of the steps in order. Let’s see how to manage each step such that we can make pasta in an efficient manner.

In the above code, I am using “map” to fire initial steps that can run parallel (line 68). After all the initial steps are completed, I am using “mapSeries” to run the rest of the steps in series (line 75). I am also printing the times it took to run the first set of events and the next set of events. The time to make pasta this way will only be 23 minutes (Steps 1,2,3 in parallel). After steps 1,2 & 3 are complete, perform the rest of the steps in the series. Pasta is now ready to be served on the plate. I am going to go have some pasta now. Until then, I hope you understood bluebird’s map and mapSeries.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade