简体   繁体   English

如何解决一致返回错误是 eslint?

[英]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.

相关问题 带有Promise的ESLint一致返回错误 - ESLint consistent-return error with Promise 如何使用if-error-return模式满足eslint中的“一致返回”规则? - How to satisfy the “consistent-return” rule in eslint using an if-error-return pattern? ESLint一致返回和嵌套回调 - ESLint consistent-return and nested callbacks 返回false是否可以通过异步函数传递ESLint一致返回错误? - Is it ok to return false, to pass ESLint consistent-return error with asynchronous functions? 预期在箭头函数的结尾处返回一个值。 (一致返回) - Expected to return a value at the end of arrow function. (consistent-return) 预期在箭头函数的末尾返回一个值consistent-return - Expected to return a value at the end of arrow function consistent-return THEN语句以promise / always return和consistent-return错误返回 - THEN statement returning with promise/always return and consistent-return bugs 我如何更改eslint解决设置 - How I can change eslint resolve settings 使用简单箭头 function 修复“一致返回”linter 问题 - Fixing 'consistent-return' linter issue with simple arrow function Firebase云功能警告:“箭头功能预期没有返回值一致 - 返回” - Firebase Cloud Functions warning: “Arrow function expected no return value consistent-return”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM