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.

/**
 * @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 ?


Solution 1:

You pass an arrow function to new Promise.

If document.querySelector(selector) is true, then you enter an if block and there is a return statement.

If it isn't true, then you go through the rest of the function and there isn't a return statement.

So sometimes there is a return and sometimes there isn't.

To be constant, there should always be a return.

So add one at the end of the function.