title: How to create a buffer promise function
body: <p>I'm doing a challenge and I have to create a promise buffer, but I'm trying everything and nothing works. This is the link for the challenge: <a href="https://jsbin.com/pefagis/edit?html,js,output" rel="nofollow noreferrer">https://jsbin.com/pefagis/edit?html,js,output</a></p>
  <p>Could someone help, I don't want the answer, I just need to know how to approach this solution.</p>
  <p>Here's the function that I need to modify</p>
  <pre class="lang-js prettyprint-override"><code>function bufferPromise(promiseFactoryFn, maxActive = 5) {
    // TODO: only call the promiseFactoryFn up to maxActive times
    // before buffering further calls.
    // The returned Promise should hide the buffering process.
    return (...args) =&gt; promiseFactoryFn(...args);
  }
  </code></pre>
  <p>And here's the &quot;solution&quot; that I could think of</p>
  <pre class="lang-js prettyprint-override"><code>function bufferPromise(promiseFactoryFn, maxActive = 5) {  
    const state = {
      active: 0,
      current: null,
      buffer: []
    }
    return (...args) =&gt; {
      state.active = state.active + 1;
      const getPromise = () =&gt; promiseFactoryFn(...args).then((response) =&gt; {
        state.active = state.active - 1;
        return response
      })
      
      if(!state.current){
        state.current = getPromise();
        return state.current;
      }
      
      if(state.active &lt;= maxActive){      
        return getPromise()
      }else{
        state.buffer.push(getPromise);      
        return state.current
      }
    }
  }
  
  
  </code></pre>