My Take on Async JavaScript Promises with Concurrency Limit
Tags: javascript, beginners, promise, concurrency
Published: 2019-10-21 03:36:19

---
title: My Take on Async JavaScript Promises with Concurrency Limit
published: true
description: I failed a tech interview recently where the question asked me to come up with a solution for this problem.
tags: #javascript, #beginners, #promise, #concurrency
---

In a recent interview, I was asked to go under the hood of a Promise and explain how it would work with a concurrency limit. Having only focused on how Async/Await made everything so much easier to read and write, I blanked out. While I did my best to attempt at it, I couldn't explain it well enough to pass the tech interview.

_Disclaimer: Below is my first impression and attempt at comprehending what a Promise and Concurrency really are. I still have much to go on and am really open to any corrections/advice to this post. Sources linked below._

<br>
###Promise?
Okay, a Promise is an object to be fulfilled at a later time provided by the Web API. It can be resolved or rejected. If resolved, a `.then` chain can be run on the result. If rejected, a `.catch` will handle the errors. Both `.then` and `.catch` takes a callback functions and let you manage your Promise however you like.
Paraphrase: As best explained by Kyle Simpson, getting a Promise is like going to a burger-joint and ordering a burger. You don't get a burger instantly, as they have to make-to-order. Instead, you get a receipt, which then lets you exchange it for the burger you order once it is ready.

The structure of a promise is like this (as shown by Beau):

```javascript
   //structure of a Promise
   let myPromise = new Promise(function(resolve, reject) {
      if (conditions are met) {
         resolve('Done!')
      } else {
         reject('Something went wrong!')
      }
   })
   //chaining a `then` when resolved
   .then(result => {return result})
   //chaining a `catch` when rejected
   .catch(error => console.log(error))
```

There's other things regarding Promises that I won't go over. Such as `try/catch` which basically lets you wrap your Async operations in a `try` block and it will call `catch` if anything inside the block goes wrong.

There's also `Async/Await`, which is like syntactic sugar for writing async functions and make them read like synchronous things. Check out the video by Brad Traversy below, it makes life simpler! I wasn't allowed to use this, though.

<br>
###Concurrency Limit
My best take on these two words is how many Promises can run or should be running at a given time interval. Given a limit (say 10 Promises at a time) and an array of Promises (say 100 elements), the solution function should run 10 Promises in parallel and queue up the next Promise should one of them complete. This repeats until all the Promises are resolved.

In comes `Promise.all()`, which is a Prototype method that returns a _super ultimate Promise_ (I'm making this up) once all the Promises are resolved.

Here's my stab at this after reading and watching some materials. I did even worse in the interview.
**Warning:** Broken code below!

```javascript
const limit = 10
let promiseBatches = []
let promiseArray = [] //realistically would be filled with Promises to be resolved
function promiseBatch(promiseArray, limit) {
   let batch = []
   i = 0
   //run 10 promises while there are still 10 promises
   while i < limit && promise.Array.length > 0 {
      let promise = promiseArray.shift()
      i ++
      batch.push(promise.resolve())
   }
   //adds batch to big batches array for Promise.all()
   promiseBatches.concat(batch)

   //recursive until promiseArray is empty, else return Promise.all() of the batches
   if promiseArray.length > 0 {
      promiseBatch(promiseArray, limit)
   } else {
      return Promise.all(promiseBatches)
   }
}
```

I'm pretty sure I'm still wrong. Any thoughts?

####Sources
[Promises explained by Beau from FreeCodeCamp](https://www.youtube.com/watch?v=IGYxfTTpoFg)

[Event Loop by Philip Roberts](https://youtu.be/8aGhZQkoFbQ)

[Advanced Async and Concurrency Patterns by Kyle Simpson](https://youtu.be/Qg1SvpIau6U)

[Async/Await by Brad Traversy](https://youtu.be/PoRJizFvM7s)