title: Express.js and Bluebird - Handling the promise chain
body: <p>In a backend API I have a login route which should perform the following sequence of actions:</p>
  
  <ul>
  <li><p>Given an username and password, try to authenticate the user against an Active Directory. If authentication has failed reply with status 401. If success, continue.</p></li>
  <li><p>Look for an user with the given username in the database. If not found reply with status 403, otherwise continue.</p></li>
  <li><p>Find if the user document has some details like email, display name, etc (in case this is not the first time logging in). If yes reply with the user object, otherwise continue.</p></li>
  <li><p>Get user details from the Active Directory and update the user object in the database. Reply with the updated object.</p></li>
  </ul>
  
  <p>Code:</p>
  
  <pre><code>router.post('/login', (req, res, next) =&gt; {
  
    // capture credentials
    const username = req.body.username;
    const password = req.body.password;
    let user = null;
  
    // authenticate
    ad.authenticate(username, password)
      .then((success) =&gt; {
        if (!success) {
          res.status(401).send(); // authentication failed
          next();
        }
        return User.findOne({ username }).exec();
      })
  
      .then((found) =&gt; {
        if (!found) {
          res.status(403).send(); // unauthorized, no account in DB
          next();
        }
        user = found;
        if (user.displayName) {
          res.status(201).json(user); // all good, return user details
          next();
        }
        // fetch user details from the AD
        return ad.getUserDetails(username, password);
      })
  
      .then((details) =&gt; {
        // update user object with the response details and save
        // ...
        return user.save();
      })
  
      .then((update) =&gt; {
        res.status(201).json(update); // all good, return user object
        next();
      })
  
      .catch(err =&gt; next(err));
  
  });
  </code></pre>
  
  <p>Now I had this running with callbacks but it was really nested. So I wanted to give Bluebird promises a try, but I have two problems:</p>
  
  <ul>
  <li><p>Looks chaotic, any better way to chain the calls and handle responses?</p></li>
  <li><p>Whenever I call <code>next()</code> to stop the request after replying, the execution continues to the other <code>.then()</code>. Although the client receives the correct response, in the server log I find that the execution have continued. For example, if there is no account in DB for a given user, the client receives the <code>403</code> response but in the server log I see an exception <code>failed to read property displayName of null</code>, because there was no user and it should have stopped in the <code>next()</code> after <code>res.status(403).send();</code>.</p></li>
  </ul>