Skip to content

Enhancement: no-misused-promises should not flag functions whose contents are wrapped in try/catch #9319

Closed as not planned
@benmccann

Description

@benmccann

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

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/no-misused-promises/

Description

The no-misused-promises documentation does not explain the motivation for the rule, so I'm left to infer that it is because promise rejections are not handled properly. There are a couple references to no-floating-promises in the docs, which support this view. However, if a function is wrapped entirely in a try/catch then we know it can never error and so any triggering of this rule would be a false positive

Fail

/** @type {NodeJS.Timeout} */
let timeout;

/** @type {() => Promise<boolean>} */
async function check() {
	clearTimeout(timeout);

	// this line would fail because check is async and setTimeout expects a synchronous function
	if (interval) timeout = setTimeout(check, interval);

	const res = await fetch(`${assets}/${__SVELTEKIT_APP_VERSION_FILE__}`, {
		headers: {
			pragma: 'no-cache',
			'cache-control': 'no-cache'
		}
	});

	if (!res.ok) {
		return false;
	}

	const data = await res.json();
	const updated = data.version !== version;

	if (updated) {
		set(true);
		clearTimeout(timeout);
	}

	return updated;
}

// this line would fail because check is async and setTimeout expects a synchronous function
if (interval) timeout = setTimeout(check, interval);

Pass

/** @type {NodeJS.Timeout} */
let timeout;

/** @type {() => Promise<boolean>} */
async function check() {
	try {
		clearTimeout(timeout);

		// this can't fail since the contents of check is wrapped in a try/catch
		if (interval) timeout = setTimeout(check, interval);

		const res = await fetch(`${assets}/${__SVELTEKIT_APP_VERSION_FILE__}`, {
			headers: {
				pragma: 'no-cache',
				'cache-control': 'no-cache'
			}
		});

		if (!res.ok) {
			return false;
		}

		const data = await res.json();
		const updated = data.version !== version;

		if (updated) {
			set(true);
			clearTimeout(timeout);
		}

		return updated;
	} catch {
		return false;
	}
}

// this can't fail since the contents of check is wrapped in a try/catch
if (interval) timeout = setTimeout(check, interval);

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: plugin rule optionNew rule option for an existing eslint-plugin rulelocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-pluginwontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions