Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:如何为末尾没有“fail”块的承诺链进行lint?_Javascript_Typescript_Jslint_Q_Flowtype - Fatal编程技术网

Javascript:如何为末尾没有“fail”块的承诺链进行lint?

Javascript:如何为末尾没有“fail”块的承诺链进行lint?,javascript,typescript,jslint,q,flowtype,Javascript,Typescript,Jslint,Q,Flowtype,如何通过lint我的Javascript代码来识别在末尾没有失败块的承诺链(promise.then().then().then()…)?现有的工具(JSHint、JSLint、流静态类型检查器、Typescript等等)是否允许这样做 坦率地说,这样做的动机是,有时我只是忘记在我编写的代码中放一个。然后,如果该代码中发生错误,它将以静默方式失败,这可能是一个需要调试的麻烦。相反,软件工程最好能够在lint时间捕获这些错误,就像我可以使用linting识别变量名中的拼写错误一样 例如: q(th

如何通过lint我的Javascript代码来识别在末尾没有失败块的承诺链(
promise.then().then().then()…
)?现有的工具(JSHint、JSLint、流静态类型检查器、Typescript等等)是否允许这样做

坦率地说,这样做的动机是,有时我只是忘记在我编写的代码中放一个。然后,如果该代码中发生错误,它将以静默方式失败,这可能是一个需要调试的麻烦。相反,软件工程最好能够在lint时间捕获这些错误,就像我可以使用linting识别变量名中的拼写错误一样

例如:

q(this_function_returns_a_promise())
.then(function(data) {
  console.log('The promise returned some data: ' + JSON.stringify(data));
})
// .. more thens, spreads, alls
.then(function(modified_data) {
  this_function_will_throw_an_error(modified_data);
})
// .. more thens, spreads, alls
.then(function(modified_modified_data) {
  console.log('I will not be printed, logging and execution will just stop in ' +
    'the middle then()');
});
// if only there was a .fail(function(err) { ... }) here that printed the error!

这不是您可以通过静态分析进行筛选或识别的东西。有许多(大多数?)情况下,承诺链不提供捕获,也不需要或不想提供捕获,因为捕获将发生在下游某处

您需要做的是使用promise库的工具来报告“僵尸”承诺或未经批准的拒绝承诺。这将发生在运行时,而不是“编译”时。如何做到这一点的细节因库而异。一些浏览器已经开始实现这方面的功能。在Chrome中,尝试在控制台中键入以下内容:

> Promise.resolve().then(function() {throw "throw";})
< Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined}
< Uncaught (in promise) throw
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Promise.resolve().then(函数(){throw“throw”;})
这不是我以前考虑过的问题,所以我不能给出直接的答案,但如果没有明显的选项,那么您最好的选择可能是eshint,因为这允许相对容易地添加自定义规则,不像您提到的大多数其他选项。@Simba:您的意思是?(我找不到“eshint”)(看起来很酷,顺便说一句)@t.J.Crowder-嗯,是的,这就是我的意思。(为了我自己的利益,打字太快了;对不起!)您希望如何区分
return promise.then().then()在没有
失败
部分的情况下,哪一项正常?将其标记为正确,因为这似乎是最佳选项。撇开像Javascript这样的语言中无失败捕获的承诺链的实用性不谈,它仍然是一件有价值的事情,因为它有助于避免错误。在承诺链不需要承诺链的情况下(例如,返回消费承诺的方法),则将该承诺链标记为未失败的linter注释将满足相同的目的。