4

When I defined my async function as :

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);
}

I get an eslint warning :

19:34  warning  Unexpected unnamed async function                          func-names

Is it a bad writing ?

thanks for feedback

****update 1**. as per Ankit's comment..**.

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

solving one warning, now getting a warning + 1 error...

update2. need to change the order of definition and use ... no more warnings or error ...

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

module.exports.sendSmtpMessage = async function sendSmtpMessage(keys, mailOptions) {
    await sendMessage(keys, mailOptions);
}
  • try `async function somename(keys .....` – Jaromanda X Dec 19 '18 at 08:34
  • Don't you want to `return await sendMessage(...);` to allow consuming code to await the result? Please revise your question and remove the answer/solution (there are now duplicated code blocks for the updates). Consider adding your own answer with the final solution or improve existing answers with more elaborate explanations. – try-catch-finally Dec 19 '18 at 09:49

3 Answers3

2

To get rid of that error, you can give a name to that async function like:

var sendSmtpMessage = async function sendSmtpMessage(keys, mailOptions)
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
2

Having named exports makes it easy organize your code.

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

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

module.exports = {
  doSomething: doSomething,
  doSomethingElse: doSomethingElse
}
Dinesh Pandiyan
  • 5,814
  • 2
  • 30
  • 49
0

linted correctly, with named functions : defined then exported ...

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

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