Skip to content

Latest commit

Β 

History

History
52 lines (38 loc) Β· 1.5 KB

File metadata and controls

52 lines (38 loc) Β· 1.5 KB

prefer-promise-with-resolvers

πŸ“ Prefer Promise.withResolvers() when extracting resolver functions from new Promise().

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§ This rule is automatically fixable by the --fix CLI option.

Promise.withResolvers() is the standard way to create a promise together with its resolve and reject functions.

This rule only reports the old boilerplate pattern where a new Promise() executor copies resolver functions to outer variables and does nothing else.

Autofix is only applied when adjacent resolver let declarations can be removed without dropping comments or type annotations.

Examples

// ❌
let resolve;
const promise = new Promise(resolve_ => {
	resolve = resolve_;
});

// βœ…
const {promise, resolve} = Promise.withResolvers();
// ❌
let fulfill;
let fail;
const deferredPromise = new Promise((resolve, reject) => {
	fulfill = resolve;
	fail = reject;
});

// βœ…
const {promise: deferredPromise, resolve: fulfill, reject: fail} = Promise.withResolvers();

Executors that do setup work are intentionally ignored.

// βœ…
const promise = new Promise(resolve => {
	functionThatPotentiallyThrows();
	resolve();
});