node.js - 重构此函数以一致地使用“return”
问题描述
我有一个 JS 函数,我收到 eslint 错误,因为重构此函数以始终使用“return”。但看起来我总是回来。有人可以建议我如何解决这个问题吗?
function filterBooks(books, context, purpose, callback) {
if (!context && !purpose) {
logger.warn("context and purpose not found so ingnoring the filtering on context and purpose");
return callback(null, books);
} else {
if (purpose) {
filterBooksByPurpose(books, purpose);
}
if (context) {
filterBooksByContext(books, context,function(err,books){
if(err){
return callback(err, null);
}else{
books = removebooksEmptyPages(books);
return callback(null, books);
}
});
}
}
}
解决方案
该else
块没有任何返回值,但该if
块有,这就是 linter 抱怨的原因。在下面的代码块中注释
function filterBooks(books, context, purpose, callback) {
if (!context && !purpose) {
logger.warn("context and purpose not found so ingnoring the filtering on context and purpose");
return callback(null, books);
} else {
// *********** no return value in this block
if (purpose) {
filterBooksByPurpose(books, purpose);
}
if (context) {
// there is no return here as well, the callback function has return value but not for filterBooks
filterBooksByContext(books, context,function(err,books){
if(err){
return callback(err, null);
}else{
books = removebooksEmptyPages(books);
return callback(null, books);
}
});
}
}
}
推荐阅读
- javascript - 反应本机路由器不起作用
- r - 在 `ggplot` 中使用 `facet_wrap()` 时获得不同的结果
- java - 为什么 apache spark 不适用于 java 10?我们得到非法反射然后 java.lang.IllegalArgumentException
- tensorflow-estimator - 如何使用 export_savedmodel 保存和恢复 tf.estimator.Estimator 模型?
- r - 如何在 R 中的多个数据集上重复相同的选择函数,然后将它们除以行数
- c-preprocessor - #use 指令在旧版 VC6 C 代码中的含义
- actions-on-google - 日期期间系统实体解析到下一个日历年以获取非特定年份的句子
- python - Python访问错误类的属性
- vmware-clarity - 完全编程隐藏/显示路标
- amazon-web-services - AWS Lambda,使用 python 在 s3 存储桶之间复制