Linting Promises in Javascript

前端 未结 1 1275
[愿得一人]
[愿得一人] 2021-01-18 02:14

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 promi

相关标签:
1条回答
  • 2021-01-18 03:05

    @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).

    0 讨论(0)
提交回复
热议问题