Skip to content

Latest commit

 

History

History
72 lines (54 loc) · 1.75 KB

no-useless-promise-resolve-reject.md

File metadata and controls

72 lines (54 loc) · 1.75 KB

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

💼 This rule is enabled in the ✅ recommended config.

🔧 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.

Fail

const main = async foo => {
	if (foo > 4) {
		return Promise.reject(new Error('🤪'));
	}

	return Promise.resolve(result);
};

async function * generator() {
	yield Promise.resolve(result);
	yield Promise.reject(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.finally(() => Promise.reject(new Error('oh no')));

Pass

const main = async foo => {
	if (foo > 4) {
		throw new Error('🤪');
	}

	return result;
};

async function * generator() {
	yield result;
	throw error;
}

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

		throw new Error('odd number');
	});
	.catch(error => {
		throw new FancyError(error);
	});

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