question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule change request: no-async-promise-executor should be if-async-promise-executor-then-try-catch-surrounding-await

See original GitHub issue

What rule do you want to change? no-async-promise-executor

Does this change cause the rule to produce more or fewer warnings? Same result with fewer warnings

How will the change be implemented? (New option, new default behavior, etc.)? New default behavior

Please provide some example code that this change will affect:

new Promise(async (resolve, reject) => {
  try {
    const someValue = await someAsyncMethod()
    const someOtherValue = await someOtherAsyncMethod()
    return resolve(someValue + someOtherValue)
  } catch (error) {
    return reject(error)
  }
})

What does the rule currently do for this code? Warns with Promise executor functions should not be async.

What will the rule do after it’s changed? I think this rule should allow async executors whenever the await calls are within a try/catch block and promise is resolved/rejected accordingly, something like if-async-promise-executor-then-try-catch-surrounding-await. As it is, this rule is forcing me to write the above code using .then() constructs which I don’t prefer. This will still prevent situations where async executors falls into the wrong pattern (i.e. resolving/rejecting synchronously).

Are you willing to submit a pull request to implement this change? Yes

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
not-an-aardvarkcommented, Jul 16, 2019
const mainAsyncMethod1 = async () => {
  const someValue = await someAsyncMethod()
  const someOtherValue = await someOtherAsyncMethod()
  return someValue + someOtherValue;
}

const mainAsyncMethod2 = async () => {
  const someValue = await someAsyncMethod()
  const someOtherValue = await someOtherAsyncMethod()
  return someValue + someOtherValue
}

const run = async () => {
  try {
    const value1 = await mainAsyncMethod1()
    const value2 = await mainAsyncMethod2()
    console.log(`Result is ${value1 + value2}`)
  } catch (error) {
    console.log(error)
    process.exit(1)
  }
}

run()
2reactions
not-an-aardvarkcommented, Jul 15, 2019

Could you replace your code with this instead?

(async (resolve, reject) => {
  const someValue = await someAsyncMethod()
  const someOtherValue = await someOtherAsyncMethod()
  return someValue + someOtherValue
})()

That would have the same behavior. It seems like using the Promise constructor is unnecessary if you’re already using async functions to produce the result.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-async-promise-executor
The new Promise constructor accepts an executor function as an argument, which has resolve and reject parameters that can be used to control...
Read more >
Async promise executor functions
I'm using ESLint that warn me: "Promise executor functions should not be async" that is the no-async-promise-executor rule that throw an error ...
Read more >
Enable no-async-promise-executor linting rule
The purpose of this rule is to improve error handling by preventing an anti-pattern: If an async executor function throws an error, the...
Read more >
Enable ESLint rule no-async-promise-executor on disabled ...
This bug is for browser/ and toolkit/. Some notes about this rule (from ESlint's site):. The Promise executor function can be an async...
Read more >
14 Linting Rules To Help You Write Asynchronous Code in ...
ESLint rules for asynchronous code · 1. no-async-promise-executor · 2. no-await-in-loop · 3. no-promise-executor-return · 4. require-atomic-updates · 5. max-nested- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found