no-promise-executor-return

禁止从 Promise 执行器函数返回值

💡 hasSuggestions

此规则报告的一些问题可通过编辑器建议手动修复

new Promise 构造函数接受一个参数,称为执行器。

¥The new Promise constructor accepts a single argument, called an executor.

const myPromise = new Promise(function executor(resolve, reject) {
    readFile('foo.txt', function(err, result) {
        if (err) {
            reject(err);
        } else {
            resolve(result);
        }
    });
});

执行器函数通常会启动一些异步操作。完成后,执行程序应调用 resolve 并返回结果,如果发生错误则调用 reject

¥The executor function usually initiates some asynchronous operation. Once it is finished, the executor should call resolve with the result, or reject if an error occurred.

执行器的返回值被忽略。从 executor 函数返回一个值是一个可能的错误,因为返回的值不能被使用并且它不会以任何方式影响 Promise。

¥The return value of the executor is ignored. Returning a value from an executor function is a possible error because the returned value cannot be used and it doesn’t affect the promise in any way.

规则详情

¥Rule Details

此规则不允许从 Promise 执行程序函数返回值。

¥This rule disallows returning values from Promise executor functions.

只允许没有值的 return,因为它是一个控制流语句。

¥Only return without a value is allowed, as it’s a control flow statement.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-promise-executor-return: "error"*/

new Promise((resolve, reject) => {
    if (someCondition) {
        return defaultResult;
    }
    getSomething((err, result) => {
        if (err) {
            reject(err);
        } else {
            resolve(result);
        }
    });
});

new Promise((resolve, reject) => getSomething((err, data) => {
    if (err) {
        reject(err);
    } else {
        resolve(data);
    }
}));

new Promise(() => {
    return 1;
});

new Promise(r => r(1));

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-promise-executor-return: "error"*/

// Turn return inline into two lines
new Promise((resolve, reject) => {
    if (someCondition) {
        resolve(defaultResult);
        return;
    }
    getSomething((err, result) => {
        if (err) {
            reject(err);
        } else {
            resolve(result);
        }
    });
});

// Add curly braces
new Promise((resolve, reject) => {
    getSomething((err, data) => {
        if (err) {
            reject(err);
        } else {
            resolve(data);
        }
    });
});

new Promise(r => { r(1) });
// or just use Promise.resolve
Promise.resolve(1);

选项

¥Options

此规则采用一个选项,即对象,具有以下属性:

¥This rule takes one option, an object, with the following properties:

  • allowVoid:如果设置为 true(默认为 false),则此规则将允许返回空值。

    ¥allowVoid: If set to true (false by default), this rule will allow returning void values.

allowVoid

使用 { "allowVoid": true } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "allowVoid": true } option:

在线运行
/*eslint no-promise-executor-return: ["error", { allowVoid: true }]*/

new Promise((resolve, reject) => {
    if (someCondition) {
        return void resolve(defaultResult);
    }
    getSomething((err, result) => {
        if (err) {
            reject(err);
        } else {
            resolve(result);
        }
    });
});

new Promise((resolve, reject) => void getSomething((err, data) => {
    if (err) {
        reject(err);
    } else {
        resolve(data);
    }
}));

new Promise(r => void r(1));

版本

此规则是在 ESLint v7.3.0 中引入。

进阶读物

资源

ESLint 中文网
粤ICP备13048890号