Linting在Javascript中的承诺 [英] Linting Promises in Javascript

查看:167
本文介绍了Linting在Javascript中的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的团队的代码库中标准化使用Q promises。

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();

并且想要一个linter来捕获任何 .then c $ c>在我们的代码中缺少一个 .catch()

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这种方法看起来总的过度。这两个函数都不能跟随任何。每个都有不同的目的:

@Jeff that approach looks as total overkill. Neither of this functions must be followed with any. Each of them has different purpose:


  • 使用 then(mapSuccess,mapFail)您想要处理解析的值,并且您需要一个将通过回调返回的值解析的结果promise。

    从技术上讲,它是一个值到其他值的映射,将由其他诺言。你可以认为它类似于数组的 map ,使用它将输入数组映射到其他是一些转换函数的结果。

  • catch(mapFail)的纯别名,然后(null,mapFail),所以只要使用它, code>,然后,但您无需传递 mapSuccess 回调。

  • ,onFail)使用简单,当你想做的是处理解析的值(不需要映射到其他promise)。 done 还将确保所有最终错误都自然地暴露(然后 catch
  • 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).

我可以想象只有一个规则,它可以是添加为linter(并且假设您使用库不记录吞咽的异常)。这是为了警告 then() catch()使用当他们的结果被忽略 done(..)或传递给其他实体进行处理。)

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

这篇关于Linting在Javascript中的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆