title: which functions done/next are bound to in mongoos pre/save/(serial/parallel) middleware
body: <p>Trying to understand mongoose middleware (pre/save/parallel) thru docs/blogs(Tim Casewell). </p>
  
  <p>Based on <a href="http://mongoosejs.com/docs/middleware.html" rel="noreferrer">http://mongoosejs.com/docs/middleware.html</a></p>
  
  <pre><code>var schema = new Schema(..);
  schema.pre('save', true, function (next, done) {
    // calling next kicks off the next middleware in parallel
    next();
    doAsync(done);
  });
  
  The hooked method, in this case save, will not be executed until done is called by each middleware.
  </code></pre>
  
  <p><em><strong>What is done/next bound to here? Can you please give a complete example of how to use it?</em></strong></p>
  
  <p>For eg: I use serial as follows:</p>
  
  <pre><code>myModel.save(function(err) {
    if (err) 
      console.error("Error Occured")
    else
      console.info("Document Stored");
  });
  
  Schema.pre('save', function(next) {
    if (!self.validateSomething()) {
      next(new Error());
    } else {
      next();
    }
  });
  </code></pre>
  
  <p><em>What is next bound to here? It needs to be bound to something to get executed? I fail to understand which function(s) next/done are referring to?</em></p>
  
  <p><em><strong>If you could elaborate control flow for my code above, that would be a great help.</em></strong></p>
  
  <p>-------------<strong>This is just to elaborate my understanding (not part of question)</strong>------</p>
  
  <pre><code>   * On executing myModel.save(...)
     * Control Flow will be passed to pre/save
     * if self.validateSomething() fails,
       * Document will not be tried to be saved in DB
       * "Error Occurred" will be printed on console
     * if validation succeeds,
       * Control Flow should be passed to *somewhere* in Mongoose libs 
         * Document will be tried to save in DB
           * On Success, "Document saved" will be printed on the console
           * On Failure, "Error Occurred" will be printed on console
  </code></pre>