<分区>
我如何检查我的 Javascript 代码以识别末尾没有失败 block 的 promise 链 (promise.then().then().then()...
)?是否有任何现有工具(JSHint、JSLint、Flow 静态类型检查器、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!