How to find missing Await on Async function calls in Node+Typescript+VSCode?
TypeScript compiler doesn't provide a compiler option for that. However, TSLint 4.4 provides an option to detect floating promises. You can read this blog post for more detailed answer: Detect missing await in typescript
Download TSLint:
npm install -g tslint typescript
Configure TSLint:
{ "extends": "tslint:recommended", "rules": { "no-floating-promises": true }}
Run TSLint:
tslint --project tsconfig.json
If there are floating promises in your code, you should see the following error:
ERROR: F:/Samples/index.ts[12, 5]: Promises must be handled appropriately
TypeScript already does this
// users is a Promise<T>const users = getUsers(); // <<< missing await// users.length is not a property of users... then and catch areconsole.log(users.length);
You can find situations where you won't be told about your mistake - where the types are compatible, for example I missed an await here:
function delay(ms: number) { return new Promise<number>(function(resolve) { setTimeout(() => { resolve(5); }, ms); });}async function asyncAwait() { let x = await delay(1000); console.log(x); let y = delay(1000); console.log(y); return 'Done';}asyncAwait().then((result) => console.log(result));
Because console.log
doesn't cause any type incompatibility between my numbers and my promises, the compiler can't tell I have made a mistake.
The only solution here would be a type annotation... but if you're gonna forget an await, you're just as likely to forget a type annotation.
let y: number = delay(1000);