[英]How can I resolve a consistent-return error is eslint?
I've got this eslint error Expected to return a value at the end of arrow function consistent-return
and even after reading the doc I still don't know how to handle it.我有这个 eslint 错误
Expected to return a value at the end of arrow function consistent-return
即使在阅读了文档后我仍然不知道如何处理它。
/**
* @param selector
* @returns {Promise<unknown>}
*/
let waitForElm = function(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
};
Any idea?任何想法?
You pass an arrow function to new Promise
.您将箭头 function 传递给
new Promise
。
If document.querySelector(selector)
is true, then you enter an if
block and there is a return
statement.如果
document.querySelector(selector)
为真,那么您输入一个if
块并且有一个return
语句。
If it isn't true, then you go through the rest of the function and there isn't a return
statement.如果不是真的,那么您通过 function 的 rest 的 go 并且没有
return
语句。
So sometimes there is a return
and sometimes there isn't.所以有时有
return
,有时没有。
To be constant, there should always be a return
.要保持不变,就应该总是有
return
。
So add one at the end of the function.所以在 function 的末尾添加一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.