# prefer-promise-try πŸ“ Prefer `Promise.try()` over promise-wrapping boilerplate. πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). `Promise.try()` is the standard way to run a callback that may either return a value, return a promise, or throw synchronously, and turn the result into a promise. This rule reports older promise-wrapping boilerplate. Autofix is only applied to `new Promise(resolve => resolve(fn()))` patterns where the timing stays synchronous. The fixed code keeps the call inside a callback so thrown errors still become promise rejections. `Promise.resolve().then(fn)` is reported but not fixed, since replacing it with `Promise.try(fn)` changes when `fn` runs and stops passing the resolved `undefined` value as an argument. ## Examples ```js // ❌ new Promise(resolve => resolve(fn())); // βœ… Promise.try(() => fn()); ``` ```js // ❌ new Promise(resolve => resolve(fn(argument))); // βœ… Promise.try(() => fn(argument)); ``` ```js // ❌ Promise.resolve().then(fn); // βœ… Promise.try(fn); ``` Executors that do other work are intentionally ignored. ```js // βœ… new Promise(resolve => { setup(); resolve(fn()); }); ```