Linting Promises in Javascript

I want to standardize the use of Q promises in my command codebase. Are there any good jsc extensions (or other linters) to help provide style when it comes to promises?

We would like our promises to follow this form:

promise() .then() .catch() .done(); 

And I would like linter to catch any .then() in our code, which is missing .catch()

Advice on other stylistic tips when it comes to promises is also welcome.

+6
source share
1 answer

@Jeff this approach looks like a complete excess. None of these functions should be accompanied by any. Each of them has a different purpose:

  • Use then(mapSuccess, mapFail) when you want to process the allowed value , and you need a promise of the result, which will be resolved using the value returned by the callback .
    Technically, this is a mapping of value to another value that will be resolved by other promises. You can think of it in the same way as the map array, with which you map the input array to another, which is the result of some transformation function.
  • catch(mapFail) is purely an alias for then(null, mapFail) , so just use it when you want then , but you don't need to pass the mapSuccess callback.
  • done(onSuccess, onFail) just use when all you want to do is process the allowed value (no need to match with other promises). done also ensures that all possible errors are naturally displayed ( then and catch , since they are mappers, swallow errors in the results of the promise).

I can only imagine one rule that can be added for linter (and it is assumed that you are using a library that does not register swallowed exceptions). It should warn about using then() or catch() when their results are ignored (they are followed by done(..) or passed to another object for processing).

+2
source

Source: https://habr.com/ru/post/985643/


All Articles