eslint警告意外的未命名异步函数,是否要摆脱它?

时间:2018-12-19 08:32:34

标签: javascript async-await eslint

当我将异步函数定义为:

module.exports.sendSmtpMessage = async function(keys, mailOptions) {  // <== warning
    await sendSmtpMessage(keys, mailOptions);
}

var sendSmtpMessage = async function(keys, mailOptions) {
...
}

update2

var sendMessage = async function sendMessage(keys, mailOptions) {
...
}

module.exports.sendSmtpMessage = async function sendSmtpMessage(keys, mailOptions) {
    await sendMessage(keys, mailOptions);
}

我收到警告:

19:34  warning  Unexpected unnamed async function                          func-names

写得不好吗?

感谢您的反馈

****更新1 **。根据Ankit的评论.. **。

module.exports.sendSmtpMessage = async function sendSmtpMessage(keys, mailOptions) {  // <== warning
    await sendSmtpMessage(keys, mailOptions);
}

var sendSmtpMessage = async function(keys, mailOptions) { // <== warning and error !
...
}

23:7   error    'sendSmtpMessage' is assigned a value but never used  no-unused-vars
23:25  warning  Unexpected unnamed async function

解决一个警告,现在得到一个警告+ 1个错误...

更新2 。需要更改定义的顺序并使用...没有更多的警告或错误...

var sendMessage = async function sendMessage(keys, mailOptions) {
...
}

module.exports.sendSmtpMessage = async function sendSmtpMessage(keys, mailOptions) {
    await sendMessage(keys, mailOptions);
}

3 个答案:

答案 0 :(得分:0)

要摆脱该错误,您可以为async函数命名,例如:

var sendSmtpMessage = async function sendSmtpMessage(keys, mailOptions)

答案 1 :(得分:0)

具有命名导出功能可以轻松组织代码。

const doSomething = async function(args) {
  // ...
}

const doSomethingElse = async function() {
    await doSomething(args);
}

module.exports = {
  doSomething: doSomething,
  doSomethingElse: doSomethingElse
}

答案 2 :(得分:0)

正确命名,带有命名函数:先定义然后导出...

const sendMessage = async function sendMessage(keys, mailOptions) {
    ....
};

module.exports.sendSmtpMessage = async function sendSmtpMessage(keys, mailOptions) {
  await sendMessage(keys, mailOptions);
};