Bluebird promises - nesting vs rejecting pattern Bluebird promises - nesting vs rejecting pattern mongoose mongoose

Bluebird promises - nesting vs rejecting pattern


I think you can get the best of both worlds by throwing and then catching your boom objects.

One thing you're missing in both approaches is that when you're already inside a then handler, the idiomatic thing to do is throw an error rather than creating and returning a rejected promise. You also don't need an else block after a return statement:

Account.findOneAsync({email: request.payload.email})  .then(function (user) {    if (user) {      return user.compareHash(request.payload.password);    }    // Account not found    throw Boom.unauthorized('Invalid username/password');  })  .then(function (validPassword) {    if (validPassword) {      return request.auth.jwt.user.sign();    }    // Invalid password    throw Boom.unauthorized('Invalid username/password');  })  .then(function (jwt) {    var response = reply.success();    return response.header('authorization', jwt);  })  .catch(function (e) {    if (e.isBoom) {      return reply(e);    }    // Perhaps log something like unhandled error    return reply(Boom.unauthorized('Invalid username/password'));  });