Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
var saveUser = function(name, profile){
  assert(typeof name === 'string')
  assert(profile instanceof Profile)
  return new bluebird.Promise(function(resolve, reject){
    // async code saving user to DB
  })
}

Or

var saveUser = function(name, profile){
  return new bluebird.Promise(function(resolve, reject){
    try {
      assert(typeof name === 'string')
      assert(profile instanceof Profile)
    } catch(err){
      return reject(err)
    }
    // async code saving user to DB
  })
}
  1. Which way is better?
  2. It is good idea to catch validation errors and throw custom ValidationError instead?
share|improve this question
    
is the commented code necessary? – Malachi Jan 30 at 15:40
    
I just added it to show posibility of catching validation errors... I am also asking which way is better... – user606521 Jan 30 at 16:54
1  
you should clean the comments out of both blocks of code and describe what they are doing, and why you wonder which is better. (Edit it into your question) – Malachi Jan 30 at 16:57
1  
I think that the exception approach has one advantage: it does not force the programmer to create callbacks for failure/success which would clutter up code. So I would use this, unless the code is asynchronous (or you already have to use callbacks for some other reason). – Attilio Jan 30 at 19:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.