Is there a tool in the Javascript or Typescript ecosystem to detect the (mis/over)use of await
on a synchronous function? (e.g. a tslint rule)
The problem came up when I mistakenly used await for the result of synchronous function,
const decoded = await jsonwebtoken.verify(session, publicKey, opts);
which my linter, the default gts tslint, didn't catch. The code oddly works (await
silently passes non-promises?), but I'd like to flag the misuse to encourage use of an asynchonous/callback option.
I had thought that such misuse of await
would trigger a Typescript error, but tsc
and ts-jest
pass the following test:
test('Demonstrate await', async () => {
function foo(): number { return 4; }
const food = await foo(); // Even replace foo() with a constant or literal.
expect(food).toBe(4);
});
Yes, it's possible to enforce a linter rule like that if you use a type system. For TSlint you can use something like this: