Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-top-level-await: Do not report cases where the promise is assigned to a variable #2516

Open
jhnns opened this issue Dec 24, 2024 · 2 comments

Comments

@jhnns
Copy link

jhnns commented Dec 24, 2024

Description

Hello 👋, thank you for this great plugin :)

I have a case where I create a Promise on the top-level and assign it to a variable. Later, in an asynchronous function, I await the promise:

const preparationDone = prepareSomething();

export async function doSomethingThatNeedsPerparation() {
	await preparationDone;
	// ...
}

The motivation behind this is as follows: I don't want to use top-level await because I don't want to slow down the evaluation of the module scope. The result of the Promise must only be available at the time when the asynchronous function is executed.

Would it therefore be possible not to report cases in which the Promise is assigned to a variable?

Fail

run().catch(error => {
	console.error(error);
	process.exit(1);
});

Pass

const resultOfRun = run();

Additional Info

I know that the rule might be "too relaxed" in that case, but I still think that my use case is valid and should not reported as an anti-pattern :)

@sindresorhus
Copy link
Owner

I agree that this is a useful pattern and should not be reported on.

@jhnns
Copy link
Author

jhnns commented Dec 24, 2024

Would you accept a PR for it? :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants