typescript-eslint: Rule proposal: functions should not be `async` unless they `await`

Before You File a Proposal Please Confirm You Have Done The Following…

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a “formatting rule”; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

The rule would check that we don’t cause unnecessary performance losses. The async keyword should not be used where TypeScript can already determine that a Promise is being returned and there is no await as adding async in that case will only cause a slight performance hit

Fail Cases

/**
 * @param {RegExp} re
 * @param {(...match: any[]) => Promise<MappedCode>} get_replacement
 * @param {string} source
 */
async function calculate_replacements(re, get_replacement, source) {
	/**
	 * @type {Array<Promise<import('./private.js').Replacement>>}
	 */
	const replacements = [];
	source.replace(re, (...match) => {
		replacements.push(
			get_replacement(...match).then((replacement) => {
				const matched_string = match[0];
				const offset = match[match.length - 2];
				return { offset, length: matched_string.length, replacement };
			})
		);
		return '';
	});
	return Promise.all(replacements);
}

Pass Cases

From https://github.com/sveltejs/svelte/blob/862949d22abf2996594b4315c04fc97e18bc4408/packages/svelte/src/compiler/preprocess/replace_in_code.js#L23:

/**
 * @param {RegExp} re
 * @param {(...match: any[]) => Promise<MappedCode>} get_replacement
 * @param {string} source
 */
function calculate_replacements(re, get_replacement, source) {
	/**
	 * @type {Array<Promise<import('./private.js').Replacement>>}
	 */
	const replacements = [];
	source.replace(re, (...match) => {
		replacements.push(
			get_replacement(...match).then((replacement) => {
				const matched_string = match[0];
				const offset = match[match.length - 2];
				return { offset, length: matched_string.length, replacement };
			})
		);
		return '';
	});
	return Promise.all(replacements);
}

Additional Info

There is https://typescript-eslint.io/rules/promise-function-async, but it’s almost exactly opposite of this. I recently turned on a handful of promise-related rules in sveltejs/svelte, but got a ton of pushback on @typescript-eslint/promise-function-async. The consensus seemed to be that the rule seemed like bad practice. The description of the rule in the docs doesn’t seem to match at all what it actually does. I think either its docs could be improved or that rule could be removed

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

I’m not sure what that means.

This is what I mean: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#timing, https://blog.izs.me/2013/08/designing-apis-for-asynchrony/. You should not have a function that’s “sometimes synchronous, sometimes asynchronous” because that makes it hard to orchestrate task order.

async function get(thing) {
  return fetch(`${base}/${thing}`).then(async (r) => r.json());
}

Why not

async function get(thing) {
  const res = await fetch(`${base}/${thing}`);
  return await r.json();
}

? Also you might be overthinking about the impact of rewrapping promises—engines are very good at optimizing that away.

Ah, I think there was some confusion because when I said “though that also makes me wonder if the @typescript-eslint/require-await behavior should just be that of the base rule by default” then I was really asking if we even need @typescript-eslint/require-await at all

But yeah, I think the typescript version actually is better here in most cases as it’s less restrictive in how the code is written. In the Svelte codebase there’s very little difference between the two rules so I didn’t appreciate that initially, but I tried applying the base rule in another project and some places it was a little annoying to have to switch the code in places where performance doesn’t matter like tests. And of course even in the main code it’s admittedly a micro-optimization. So thanks for talking through this and I agree with the conclusion of closing it

Hope this has been helpful in addressing your request! 🙂

yes, thank you!!

the extension uses type information

In general that’s super valuable, but for this particular rule I’m not sure the type information can be used in a way that results in a more valuable rule

what you’d have then is just a significantly-harder-to-configure, worse version of the base rule

Why would it be harder to configure? The version in this repo uses the options from the base rule, so it seems like the configuration is the same in either case