I am looking to standardize the use of Q promises in my team's codebase. Are there any good jscs extensions (or other linters) to help enforce style when it comes to promises?
We would like our promises to follow this form:
promise()
.then()
.catch()
.done();
And would like a linter to catch any .then()
in our code that is missing a .catch()
Advice for other stylistic tips when it comes to promises is welcome too.
then
s in any code should not be followed bycatch
, as youreturn
promises from your functions. You'd use this pattern (if at all) only at the end of a promise chain, and I guess that is hard to track for a simple stylechecker. – Selfevident.then()
having to have a.catch
and.done
is like making every function wrapped in atry/catch
or making every function that doesn't return a value explicitly say so. In my opinion if you want better debugging try bluebird promises instead of Q, they drop the requirement for.done
in order to show exceptions and they also perform much better. Basically, this is Qs fault for making you do this and you can avoid it with a modern library. – Mitzvah.catch
for every.then
call was that we were having exceptions being swallowed with just.then
, and catch seemed to be the way to fix it. For example, in a function that returns a deferred.promise:function() { var def = Q.defer(); promise.then(function(result){ // do something to modify result def.resolve(result); }) .catch(function(err) { def.reject(err); }) return def.promise(); }
Would this be better handled with.done
or something in BlueBird? – Hamel