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.
@Jeff that approach looks as total overkill. Neither of this functions must be followed with any. Each of them has different purpose:
- Use
then(mapSuccess, mapFail)
when you want to process resolved value and you need a result promise that will resolve with value returned by callback.
Technically it's a mapping of value into other value which will be resolved by other promise. You may think of it similarly as of array's map
with which you map input array into other which is result of some transformation function.
catch(mapFail)
is purely alias for then(null, mapFail)
, So just use it when you want to then
but you have no need to pass mapSuccess callback.
done(onSuccess, onFail)
use simply, when all you want to do is to process resolved value (no need for mapping to other promise). done
will also assure that all eventual errors are naturally exposed (then
and catch
as they're mappers, swallow errors into promise results).
I can imagine only one rule, which can be added for linter (and it's assuming that you use library which doesn't log swallowed exceptions). It's to warn about then()
or catch()
usages when their results are being ignored (they should be followed by done(..)
or passed to other entity for processing).