Can't figure how to selectively handle an error in a promise chain...
I'm not sure when should I return a new promise (with new, or promise.reject or promise.fulfill), how to break the chain (return promise.reject or throw?), and which rejection is caught by .done — and how to only handle some errors in .done?
(Localization would be nice, ie, avoiding global variables, like I had in callback hell before. Also, keep error handling near cause, unless handled collectively.)
Context: Express route handler for "/save" with somewhat complex logic, and optionally promisified LevelDB.
promise=require 'promise' # (Capitalize Promise in anticipation of ES6?)
db=(require 'level-promise') sub (require 'level') './data',keyEncoding:'utf8',valueEncoding:'json'
Simplified untested pseudocode (CoffeeScript, obviously), eg:
.post '/save',(req,res)->
new promise ->
# Validations… Sync stuff.
unless req.body.role then return promise.reject res.send 400,'Missing role.'
# Etc.
promise.fulfill()
.then ->
# Permissions, similarly, but 403...
.then ->
# But what about async stuff?
# If changing username, ensure availability.
return new promise (fulfill,reject)->
if key_change
db.get r.username,(err,v)-> # Why not promisified/denodeify?!
unless err?.notFound then reject res.send 400,'Username already taken.' # Throw instead?
else
changes.push type:'del',key:r.current_username
fulfill() # ?
else fulfill() # ?
# Or .catch here?
# .catch here?
.then ->
# Etc.
.then ->return db.batch changes # Promisified?
# Error handling for everything (that wasn't already).
.done ->res.send 200,'Saved.'
,-> # Which rejection caught here?
res.send 500,'Oops...'
# Or better?
.then ->res.send 200,'Saved.'
.catch -> # Which rejection caught here?
res.send 500,'Oops...'
.done() # Because catch doesn't end chain?
Thanks!
PS: http://www.html5rocks.com/en/tutorials/es6/promises/ seems helpful, but I still haven't digested it. Seems to suggest I need to add .catch'es inside the chain? But How to stop/break in the middle of chained promises suggests catching the inner promise instead?