NodeJS conditional request before the real request In my code I have to login a user with username and password. But if the user sends his e-mail instead of his username, I have to resolve it with another request.

This is what the code looks like:

exports.auth = (request, response, next) -&gt;
  username = request.body.username
  if request.body.email
      users.findByEmail request.body.email, (error, result) -&gt;
          return next new InternalServerError(error) if error
          username = result[0].username
          auth.auth username, request.body.password, (error, result) -&gt;
              return next new InternalServerError(error) if error
              response.json responseData
  else
      auth.auth username, request.body.password, (error, result) -&gt;
          return next new InternalServerError(error) if error
          response.json responseData


As you can see there are 3 duplicate lines. 

Does anyone know a good code style do make this better? Maybe with Promises? Async Waterfall? Waitfor?
Promises would make sense to me, but first step would be to extract stuff into functions.

You don't really need promises for this, though. Here's one way to just do it with plain callbacks:

exports.auth = (request, response, next) -&gt;
  getUsername = (request, cb) -&gt;
    email = request.body.email
    return cb null, request.body.username unless email

    users.findByEmail email, (error, result) -&gt;
      return cb new InternalServerError(error) if error
      cb null, result[0].username

  getUsername (err, username) -&gt;
    return next err if err
    auth.auth username, request.body.password, (err, json) -&gt;
      return next err if err
      response.json json