Skip to content

Latest commit

Β 

History

History
84 lines (66 loc) Β· 1.88 KB

File metadata and controls

84 lines (66 loc) Β· 1.88 KB

no-useless-promise-resolve-reject

πŸ“ Disallow returning/yielding Promise.resolve/reject() in async functions or promise callbacks.

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

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

Wrapping a return value in Promise.resolve in an async function or a Promise#then/catch/finally callback is unnecessary as all return values in async functions and promise callback functions are already wrapped in a Promise. Similarly, returning an error wrapped in Promise.reject is equivalent to simply throwing the error. This is the same for yielding in async generators as well.

Examples

// ❌
const main = async foo => {
	if (foo > 4) {
		return Promise.reject(new Error('πŸ€ͺ'));
	}

	return Promise.resolve(result);
};

// βœ…
const main = async foo => {
	if (foo > 4) {
		throw new Error('πŸ€ͺ');
	}

	return result;
};
// ❌
async function * generator() {
	yield Promise.resolve(result);
	yield Promise.reject(error);
}

// βœ…
async function * generator() {
	yield result;
	throw error;
}
// ❌
promise
	.then(x => {
		if (x % 2 == 0) {
			return Promise.resolve(x / 2);
		}

		return Promise.reject(new Error('odd number'));
	})
	.catch(error => Promise.reject(new FancyError(error)));

// βœ…
promise
	.then(x => {
		if (x % 2 == 0) {
			return x / 2;
		}

		throw new Error('odd number');
	});
	.catch(error => {
		throw new FancyError(error);
	});
// ❌
promise.finally(() => Promise.reject(new Error('oh no')));

// βœ…
promise.finally(() => {
	throw new Error('oh no');
});