Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

@typescript-eslint/no-floating-promises with const function declaration

I’m having trouble understanding why the @typescript-eslint/no-floating-promises works with some, but not all my async functions.

In my understanding, the following 2 functions are equivalent

const getUser = async (userId: string): Promise<User> => {
  const userRef = await getUsersCollection().doc(userId).get()
  return userRef.data()
}

async function getUser2(userId: string) {
  const userRef = await getUsersCollection().doc(userId).get()
  return userRef.data()
}

How ever when I run npm lint (or look at my code in VS Code) only one call triggers the no floating promises error – why?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

Because you didn’t assign the result of the promise to anything.

Either assign it

const x = getUser2()

or handle it

getUser2().then(() => do something).catch(() => do something else)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading