title: Express.js next like functionality using a generator
body: <p>I have a list of functions <code>functions</code>. Each of these functions take <code>x</code>(irrelevant) and <code>next</code> as arguments. I'm trying to implement something like a <code>middleware</code> as present in <code>express.js</code>. Each of the middlewares run and pass contol to the next <code>middleware</code> by calling <code>next</code>.</p>
  
  <pre><code>    let x;
      function* generator (functions, next) =&gt; {
         for (let _function of functions){
           yield _function(x,next);
         }
      }
      function next(generator){
        generator.next();
      }
      //cant quite decide on how to write the below lines
      //I can't bind the generator to next because it is not yet defined
      //
      generator(functions1, next.bind('???'));
      generator(functions2, next);
      generator(functions3, next);
  </code></pre>
  
  <p><code>functions{1,2,3}</code> are a list of middlewares.
  Each of the functions in the middleware would look like </p>
  
  <pre><code>f(x, next){
     //do things with x
     next();
     //maybe do more things
  }
  </code></pre>
  
  <p>This is something I tried entirely on my own. I'm wondering if there is an altogether better way of doing this. I'm looking for plain js implementations without using libraries.</p>