How to find missing Await when calling Async function in Node + Typescript + VSCode?

We deployed errors in our b / c node application, we forgot the prefix of asynchronous function calls with "wait".

Example:

const getUsers = async () => db.query('SELECT * from Users');

const testMissingAwait = async () => {
  const users = getUsers(); // <<< missing await
  console.log(users.length);
};

testMissingAwait();

Is there an easy way to find async function calls missing from the await keyword?

Otherwise, how much effort would it be to write a Visual Studio Code extension that automatically labels them? (I'm going to tackle if anyone can point me in the right direction).

+4
source share
2 answers

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 are
console.log(users.length);

You may find situations where you are not told about your mistake - where the types are compatible, for example, I missed the wait 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));

console.log promises, , .

... , .

let y: number = delay(1000);
+3

TypeScript . TSLint 4.4 promises. , : typescript

TSLint:

npm install -g tslint typescript

TSLint:

{
    "extends": "tslint:recommended",
    "rules": {
        "no-floating-promises": true
    }
}

TSLint:

tslint --project tsconfig.json

promises, :

: F:/Samples/index.ts [12, 5]: promises

+1

Source: https://habr.com/ru/post/1687676/


All Articles