2

Suppose I have code like this, assuming db.get() is a promisified API:

var promises = [];
for( var i = 0; i < 10; i++ ){
    promises.push( getFromDatabase("age") );
}
Q.all(promises).then(function(results){
    // something
},function(error){
    log.error( error )
});

function getFromDatabase(property){
    return db.get(property).then(function(result){
        return result;
    },function(error){
        log.error( error )
    })
}  

Who'll handle the error? The function in promise chain of Q.all() or the function in promise chain of getFromDatabase().

Also, if there is a top level error handler like in Q.all(), is there a need for handling error in helper functions?

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • You probably should pass `null` instead of the identity function to `then`, or just use `catch`. – Bergi May 22 '16 at 20:40
  • See also [this question about chaining `catch`](http://stackoverflow.com/questions/16371129/chained-promises-not-passing-on-rejection). – Bergi May 22 '16 at 20:40

1 Answers1

2

Who'll handle the error? The function in promise chain of Q.all() or the function in promise chain of getFromDatabase().

With your code in the question, errors will be handled by getFromDatabase()'s error handlers. But in your case, errors will also not propagate further to Q.all()'s one, because you basically handler error there and never pass error futher.

Also, if there is a top level error handler like in Q.all(), is there a need for handling error in helper functions?

It depends on your business logic, what behaviour you want from your app. Maybe you want to log error in inner getFromDatabase error handler, and pass failure further to the outer handler for proper error message for UI rendering. Up to you. But in this case you would need to rethrow error or return rejected promise from inner handler:

Q.all(promises).then(function(results){
    // something
},function(error){
    // Render error message for UI, etc.
});

function getFromDatabase(property){
    return db.get(property).then(function(result){
        return result;
    },function(error){
        // just log here
        log.error(error);
        throw error;
    })
} 
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • so `function(err){}` is more like `try-catch`. If it's handled deep within and now thrown, it wont be propagated to the top. right? – An SO User May 22 '16 at 14:58
  • Yes, in order to pass error to the next handler in the chain (to the top) you need either to return rejected promise or throw. Not returning anything like in your code is equivalent to resolved promise which means you handled error. – dfsq May 22 '16 at 15:00
  • what if the helper function doesnt have an error handler? – An SO User May 22 '16 at 15:06
  • You mean `getFromDatabase` function? Then error directly bubbles to the next error handler in chain, in your case `Q.all`. – dfsq May 22 '16 at 15:20
  • So it's much cleaner to just skip inner error handlers and let it bubble to the top, right? – An SO User May 22 '16 at 15:32
  • Yes, if you are not interested in handling error for specific request in its own error handler. – dfsq May 22 '16 at 15:42